Iphone UIView旋转可以';这不会发生两次
在我的UITableViewCell中,我有UIImageView,每次用户单击该行时,我都要将其旋转180°(didSelectRowatineXpath:)。代码非常简单:Iphone UIView旋转可以';这不会发生两次,iphone,ios,rotation,core-graphics,Iphone,Ios,Rotation,Core Graphics,在我的UITableViewCell中,我有UIImageView,每次用户单击该行时,我都要将其旋转180°(didSelectRowatineXpath:)。代码非常简单: - (void) tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath { UITableViewCell *curCell = [self.tableView cellForRowAtIndex
- (void) tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
UITableViewCell *curCell = [self.tableView cellForRowAtIndexPath:indexPath];
UIImageView *imgArrow = (UIImageView*)[curCell viewWithTag:3];
[UIView animateWithDuration:0.3 animations:^{imgArrow.transform = CGAffineTransformMakeRotation(M_PI);}];
}
问题是,这种情况总是只发生一次——用户第一次单击单元格时,imgArrow会正确旋转,但第二次单击单元格时,imgArrow不会向后旋转。为什么?
谢谢你的帮助 您只是在设置转换。要应用多个变换,必须将
imgArrow.transform
变换矩阵乘以所需的新变换。您可以使用CGAffineTransformConcat()
执行此操作
CGAffineTransform currTransform = [imgArrow transform];
CGAffineTransform newTransform = CGAffineTransformConcat(currTransform, CGAffineTransformMakeRotation(M_PI));
[imgArrow setTransform:newTransform];
问题在于,“视图变换”特性旋转到从“视图原始变换”指定的程度。所以,一旦你的按钮旋转180度,再次调用它将不会起任何作用,因为它将尝试从当前的位置(180度)旋转到180度 也就是说,您需要使用if语句来检查转换。如果为180,则将旋转设置为“0”,反之亦然 实现这一点的简单方法是使用
BOOL
if (shouldRotate){
[UIView animateWithDuration:0.3 animations:^{imgArrow.transform = CGAffineTransformMakeRotation(M_PI);}];
shouldRotate = NO;
}else{
[UIView animateWithDuration:0.3 animations:^{imgArrow.transform = CGAffineTransformMakeRotation(0);}];
shouldRotate = YES;
}