CGAffineTransformMakeRotation counter clockwise always

前端 未结 2 1646
南笙
南笙 2020-12-10 06:50

I an trying to animate a UIImageView to rotate counter clockwise when a user touches it and moves its finger on it in clockwise direction.
Basically I want

相关标签:
2条回答
  • 2020-12-10 07:01

    The technique I've used is to divide the rotation angle by 2 and separate the rotate into two separate animations, linked by the completion: parameter of animateWithDuration:delay:options:animations:completion:. But, since the view has been rotated to its current position, you need to start with that position and "unrotate" back to zero, vs attempting to rotate CCW from zero.

    0 讨论(0)
  • 2020-12-10 07:25

    I had a similar issue, check the accepted answer out, it might help you:

    Rotate a UIView clockwise for an angle greater than 180 degrees


    In response to comments, maybe this will help:

    In my code I actually use

    rotationAnimation.fromValue
    rotationAnimation.toValue
    

    instead of

    rotationAnimation.byValue.
    

    The rotation is always going to be counter clockwise if the toValue is less than the fromValue. It doesn't matter if your values are positive or negative, only the relation between them.

    Figure out what your starting angle is, figure out which direction you want to rotate and figure out what your ending angle is. Make sure that it's less than your starting angle if you want to go counter clockwise. Here is the code I use for animating a clock hand clockwise from 12:00 to the current time.

    -(void) viewDidAppear:(BOOL)animated
    {
        [super viewDidAppear:animated];
    
        [self updateClockArmAngle];
    }
    
    - (void)updateClockArmAngle
    {
        // Get the time
        NSDate *date = [NSDate date];
        NSCalendar* calendar = [[[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar] autorelease];
        [calendar setTimeZone:[NSTimeZone timeZoneWithName:@"America/Toronto"]];
        NSDateComponents* components = [calendar components:(NSHourCalendarUnit | NSMinuteCalendarUnit) fromDate:date];
    
        CGFloat hour = [components hour];
        CGFloat angle = (hour/24.0)*(2*M_PI);
    
        CGFloat minute = [components minute];
        angle += (minute/(24*60))*(2*M_PI);
    
        [self rotateViewAnimated:self.clockArm withDuration:1.5 byAngle:angle];
    }
    
    
    - (void) rotateViewAnimated:(UIView*)view
                   withDuration:(CFTimeInterval)duration
                        byAngle:(CGFloat)angle
    {    
        CABasicAnimation *rotationAnimation;
        rotationAnimation = [CABasicAnimation animationWithKeyPath:@"transform.rotation.z"];
        rotationAnimation.fromValue = 0;
        rotationAnimation.toValue = [NSNumber numberWithFloat:angle];
        rotationAnimation.duration = duration;
        rotationAnimation.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseInEaseOut];
        [rotationAnimation setRemovedOnCompletion:NO];
        [rotationAnimation setFillMode:kCAFillModeForwards];
        [view.layer addAnimation:rotationAnimation forKey:@"rotationAnimation"];
    }
    
    0 讨论(0)
提交回复
热议问题