开发者

Issue rotating a UIView object using Core Animation and blocks

I have a problem with a UIView-subclass object that I am rotating using Core Animation in response to a UISwipeGesture.

To describe the context: I have a round dial that I have drawn in CG and added to the main view as a subview. In response to swipe gestures I am instructing it to rotate 15 degrees in either direction dependent on whether it;s a left or right swipe.

The problem that it will only rotate each way once. Subsequent gestures are recognised (evident from other actions that are triggered) but the animation does not repeat. I can go left once then right once. But trying to go in either direction multiple times doesn't work. Here's the relevant code, let me know your thoughts...

- (IBAction)handleLeftSwipe:(UISwipeGestureRecognizer *)sender 
{
    if ([control1 pointInside:[sender locationInView:control1] withEvent:nil]) 
    {
    //updates the display value
    testDisplay.displayValue = testDisplay.displayValue + 0.1;
    [testDisplay setNeedsDisplay];

    //rotates the dial
    [UIView animateWithDuration:0.25 animations:^{
        CGAffineTransform  xfo开发者_Python百科rm = CGAffineTransformMakeRotation(radians(+15));
        control1.transform = xform;
        [control1 setNeedsDisplay];
    }];
} 


CGAffineTransform xform = CGAffineTransformMakeRotation(radians(+15));

Do you keep a total of how far the rotation is. CGAffineTransformMakeRotation are not additive. Only the most recent is used. So you are setting it to 15 each time, not 15 more each time.


Here's a super simple example of rotating a view cumulatively. This rotates the view by 180 degrees each button press.

    - (IBAction) onRotateMyView: (id) sender
    {

     [UIView animateWithDuration:0.3 animations:^{
          myView.transform = CGAffineTransformMakeRotation(M_PI/2*rotationCounter);
     } completion:^(BOOL finished){
        //No nothing
     }];

     ++rotationCounter;
}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜