[英]Iphone-How to rotate image continuously for 30 secs and stop at 180 degrees
[英]Iphone-How do I get an image to rotate continously for about 60 seconds and then stop in the other direction
我需要使图像旋转几次并以相反的方向停止。 图像是一个带有箭头的按钮,该箭头指向右,然后旋转几次,然后以另一种方式停下来。 任何帮助将不胜感激。 这就是我目前所拥有的。
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;
请尝试这个,让我知道。 这对我有用。
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));
以下代码对我有用。 它实际上并不会改变方向。 我认为您可以通过添加一个更改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;
}
我用这个:
- (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秒内呼叫取消动画。 您可以根据需要设置旋转角度
谢谢大家,此页面现在非常有用。
这是另一种解决方案。
- (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.