0

我需要图像旋转几次,然后在相反的方向停下来。该图像是一个带有箭头的按钮,它指向右侧,然后它旋转了几次,然后停止面向另一方向。任何帮助将不胜感激。这就是我目前所拥有的。

CAKeyframeAnimation *animation = [CAKeyframeAnimation animationWithKeyPath:@"transform.rotation.z"];
  animation.duration = 0.60;//animation.duration = 0.60;
  animation.repeatCount = 1;//animation.repeatCount = 1000ˆ

//#warning remove comment brackets below

  animation.keyTimes = [NSArray arrayWithObjects:
                        [NSNumber numberWithFloat:0.0],
                        [NSNumber numberWithFloat:1.0], nil];

  animation.values = [NSArray arrayWithObjects:
                      [NSNumber numberWithFloat:0.0],
                      [NSNumber numberWithFloat:(degrees / 180)*M_PI], nil];

  animation.fillMode = kCAFillModeForwards;
4

4 回答 4

1

请试试这个,让我知道。这对我有用。

UIImageView *tempView = [[UIImageView alloc] initWithFrame:CGRectMake(100, 100, 80, 80)];
    tempView.image = [UIImage imageNamed:@"Icon"];
    tempView.backgroundColor = [UIColor redColor];
    [self.view addSubview:tempView];

    const NSUInteger rotations = 10;
    const NSTimeInterval duration  = 5;

    CAKeyframeAnimation *anim = [CAKeyframeAnimation animationWithKeyPath:@"transform.rotation"];
    CGFloat touchUpStartAngle = 0;
    CGFloat touchUpEndAngle = (M_PI);
    CGFloat angularVelocity = (((2 * M_PI) * rotations) + M_PI) / duration;
    anim.values = @[@(touchUpStartAngle), @(touchUpStartAngle + angularVelocity * duration)];
    anim.duration = duration;
    anim.autoreverses = NO;
    anim.delegate = self;
    anim.repeatCount = 1;
    anim.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseOut];
    [tempView.layer addAnimation:anim forKey:@"test"];

    tempView.transform = CGAffineTransformMakeRotation(touchUpStartAngle + (touchUpEndAngle));
于 2013-02-07T07:57:42.313 回答
0

以下代码适用于我。它实际上并没有改变方向。我认为您可以通过添加一个更改 imagesArray 的计时器来实现这一点。首先,我也尝试像您一样使用 CAKeyframeAnimation,但我无法弄清楚。所以我改用动画图像 - 我认为这种方式不会太糟糕,因为 Apple 例如也使用这种方式来为活动指示器设置动画。

-(void) myImageInitFunction {
    UIImage *icon = [UIImage imageNamed:@"yourIconImage"];
    NSArray *imagesArray = rotatingImagesArrayOfImage(icon, 30);
    myImageViewThatShouldRotate.animationImages = imageArray;
    myImageViewThatShouldRotate.animationDuration= 2.0;
    [myImageViewThatShouldRotate startAnimating];
}

NSArray *rotatingImagesArrayOfImage(UIImage * icon, NSInteger nFrames)
{
    NSMutableArray *images = [NSMutableArray arrayWithObject:icon];

    for (NSInteger nLoop = 1; nLoop < nFrames; nLoop++) {
            [images addObject: [UIImage imageWithCGImage: CGImageRotatedByAngle(icon.CGImage, nLoop * ( 360 / nFrames)) ] ];
    }
    return images;
}

CGImageRef CGImageRotatedByAngle(CGImageRef imgRef,  CGFloat angle) {
    CGFloat angleInRadians = angle * (M_PI / 180);
    CGFloat width = CGImageGetWidth(imgRef);
    CGFloat height = CGImageGetHeight(imgRef);

    CGRect imgRect = CGRectMake(0, 0, width, height);
    CGAffineTransform transform = CGAffineTransformMakeRotation(angleInRadians);
    CGRect rotatedRect = CGRectApplyAffineTransform(imgRect, transform);

    CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceRGB();

    CGContextRef bmContext = CGBitmapContextCreate(NULL,
                                               rotatedRect.size.width,
                                               rotatedRect.size.height,
                                               8,
                                               0,
                                               colorSpace,
                                               kCGImageAlphaPremultipliedFirst);
    CGContextSetAllowsAntialiasing(bmContext, YES);
    CGContextSetShouldAntialias(bmContext, YES);
    CGContextSetInterpolationQuality(bmContext, kCGInterpolationHigh);
    CGColorSpaceRelease(colorSpace);
    CGContextTranslateCTM(bmContext,
                      +(rotatedRect.size.width/2),
                      +(rotatedRect.size.height/2));
    CGContextRotateCTM(bmContext, angleInRadians);
    CGContextTranslateCTM(bmContext,
                      -(rotatedRect.size.width/2),
                      -(rotatedRect.size.height/2));
    CGContextDrawImage(bmContext, CGRectMake(0, 0,
                                         rotatedRect.size.width,
                                         rotatedRect.size.height),
                   imgRef);



    CGImageRef rotatedImage = CGBitmapContextCreateImage(bmContext);
    CFRelease(bmContext);

    return rotatedImage;
}
于 2013-02-07T07:48:57.247 回答
0

我用这个:

- (void)startAnimations {

   CABasicAnimation* spinAnimation = [CABasicAnimation
                                   animationWithKeyPath:@"transform.rotation"];
   spinAnimation.toValue = [NSNumber numberWithFloat:M_PI];
   spinAnimation.cumulative = YES;
   spinAnimation.duration = 0.5;
   spinAnimation.repeatCount = 10000;
   [imageView.layer addAnimation:spinAnimation forKey:@"spinAnimation"];

}
- (void)cancelAnimations {

   [imageView.layer removeAnimationForKey:@"spinAnimation"];
   imageView.transform = CGAffineTransformMakeRotation(0.0);

}

在开始动画中调用计时器以在 60 秒内调用取消动画。你可以随心所欲地制作旋转角度

于 2013-02-07T07:55:17.767 回答
0

谢谢大家这个页面现在非常足智多谋。

这是另一种解决方案。

- (CAKeyframeAnimation *)rotateFrom:(CGFloat)fromDegrees to:(CGFloat)toDegrees {
    CAKeyframeAnimation *animation = [CAKeyframeAnimation animationWithKeyPath:@"transform.rotation.z"];
    animation.delegate = self;
    animation.duration = 0.30;
    animation.repeatCount = 1;



    animation.keyTimes = [NSArray arrayWithObjects:
                          [NSNumber numberWithFloat:0.0],
                          [NSNumber numberWithFloat:1.0], nil];

    animation.values= [NSArray arrayWithObjects:
                       [NSNumber numberWithFloat:fromDegrees*(M_PI/180)],
                       [NSNumber numberWithFloat:toDegrees*(M_PI/180)], nil];

    animation.fillMode = kCAFillModeForwards;




    animation.removedOnCompletion = NO;


    return animation;


}
于 2013-02-08T05:50:19.240 回答