简体   繁体   English

Iphone-如何使图像连续旋转约60秒,然后朝另一个方向停止

[英]Iphone-How do I get an image to rotate continously for about 60 seconds and then stop in the other direction

I need the image to spin around a few times and come to a stop in the opposite dirrection. 我需要使图像旋转几次并以相反的方向停止。 THe image is a button with arrow that points right and then it spins a few times and comes to a stop facing the other way. 图像是一个带有箭头的按钮,该箭头指向右,然后旋转几次,然后以另一种方式停下来。 Any help will be appreciated. 任何帮助将不胜感激。 This is what I have at the moment. 这就是我目前所拥有的。

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;

Please try this and let me know. 请尝试这个,让我知道。 This is working for me. 这对我有用。

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));

The following code works for me. 以下代码对我有用。 It actually doesn´t change the direction. 它实际上并不会改变方向。 I think you can achieve this by adding a timer which change the imagesArray. 我认为您可以通过添加一个更改imagesArray的计时器来实现。 First I also tried using CAKeyframeAnimation like you but I couldn´t figure it out. 首先,我也尝试像您一样使用CAKeyframeAnimation,但我无法弄清楚。 So I changed to an animating image - I thought this way couldn´t be too bad, because Apple eg use this way also for animating the activity indicator. 因此,我更改为动画图像-我认为这种方式可能还不错,因为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;
}

i use this: 我用这个:

- (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);

}

call a timer in start animations to call cancelanimations in 60 secs. 在开始动画中呼叫计时器,以在60秒内呼叫取消动画。 you can make rotation angle whatever you want 您可以根据需要设置旋转角度

Thanks everyone this page is now very resourceful. 谢谢大家,此页面现在非常有用。

This is another solution. 这是另一种解决方案。

- (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;


}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM