[英]Rotation Animation on UIImageView
我有阳光图像。 我喜欢将其放在背景上,并使其以慢动作连续旋转。
图像是这个。
我曾尝试使用CABasicAnimation旋转它,但它旋转了整个框架..我只希望阳光在背景而不是整个框架上旋转。
有什么办法吗?
更新:
这是我在做什么...请指出我的错误... :(
我没有你解释过的explained .. :(
这是我在做什么...
- (void)viewDidLoad {
sunraysContainer = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 1024, 768)];
sunraysContainer.clipsToBounds = YES;
[self.view addSubview:sunraysContainer];
sunrays = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 1024, 768)];
[sunrays setImage:[UIImage imageNamed:@"sunlight-background copy2.jpg"]];
[sunraysContainer addSubview:sunrays];
backgroundBuilding = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 1024, 768)];
[backgroundBuilding setImage:[UIImage imageNamed:@"hira-background_fade.png"]];
[sunraysContainer addSubview:backgroundBuilding];
[UIView beginAnimations:nil context:nil];
[UIView setAnimationDuration:5];
CABasicAnimation *rotationAnimation;
rotationAnimation = [CABasicAnimation animationWithKeyPath:@"transform.rotation.z"];
rotationAnimation.toValue = [NSNumber numberWithFloat:M_PI * 2.0 * 4.0];
rotationAnimation.duration = 1;
rotationAnimation.cumulative = YES;
rotationAnimation.repeatCount = 10;
rotationAnimation.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseOut];
[sunrays.layer addAnimation:rotationAnimation forKey:@"rotationAnimation"];
[UIView commitAnimations];
}
更新中...
我做了你说的玉米片...谢谢你的答复,但是我得到了黑屏.. :(
我已经编码了...
sunrays = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"sunlight-background copy2.jpg"]];
UIView *container = [[UIView alloc] initWithFrame:CGRectMake(0,0,sunrays.frame.size.height,sunrays.frame.size.height)];
[container setClipsToBounds:YES];
[container addSubview:sunrays];
[sunrays setCenter:CGPointMake(container.bounds.size.width/2, container.bounds.size.height/2)];
[UIView beginAnimations:nil context:nil];
[UIView setAnimationDuration:5];
CABasicAnimation *rotationAnimation;
rotationAnimation = [CABasicAnimation animationWithKeyPath:@"transform.rotation.z"];
rotationAnimation.toValue = [NSNumber numberWithFloat:M_PI * 2.0 * 4.0];
rotationAnimation.duration = 1;
rotationAnimation.cumulative = YES;
rotationAnimation.repeatCount = 10;
rotationAnimation.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseOut];
[sunrays.layer addAnimation:rotationAnimation forKey:@"rotationAnimation"];
[UIView commitAnimations];
什么是错误的代码?
好吧,我认为您应该生成不同的图像,然后尝试对此进行动画处理
imgLoading.animationImages = [NSArray arrayWithObjects:
[UIImage imageNamed:@"Img1.png"]
[UIImage imageWithCGImage:
[self CGImageRotatedByAngle:
[[UIImage imageNamed:@"Img1.png"] CGImage]angle:30.0]],
[UIImage imageWithCGImage:
[self CGImageRotatedByAngle:
[[UIImage imageNamed:@"Img1.png"] CGImage]angle:60.0]],
[UIImage imageWithCGImage:
[self CGImageRotatedByAngle:
[[UIImage imageNamed:@"Img1.png"] CGImage]angle:90.0]],
[UIImage imageWithCGImage:
[self CGImageRotatedByAngle:
[[UIImage imageNamed:@"Img1.png"] CGImage]angle:120.0]],
[UIImage imageWithCGImage:
[self CGImageRotatedByAngle:
[[UIImage imageNamed:@"Img1.png"] CGImage]angle:150.0]],
[UIImage imageWithCGImage:
[self CGImageRotatedByAngle:
[[UIImage imageNamed:@"Img1.png"] CGImage]angle:180.0]],
[UIImage imageWithCGImage:
[self CGImageRotatedByAngle:
[[UIImage imageNamed:@"Img1.png"] CGImage]angle:210.0]],
[UIImage imageWithCGImage:
[self CGImageRotatedByAngle:
[[UIImage imageNamed:@"Img1.png"] CGImage]angle:240.0]],
[UIImage imageWithCGImage:
[self CGImageRotatedByAngle:
[[UIImage imageNamed:@"Img1.png"] CGImage]angle:270.0]],
[UIImage imageWithCGImage:
[self CGImageRotatedByAngle:
[[UIImage imageNamed:@"Img1.png"] CGImage]angle:300.0]],
[UIImage imageWithCGImage:
[self CGImageRotatedByAngle:
[[UIImage imageNamed:@"Img1.png"] CGImage]angle:330.0]],
[UIImage imageWithCGImage:
[self CGImageRotatedByAngle:
[[UIImage imageNamed:@"Img1.png"] CGImage]angle:360.0]], nil];
imgLoading.animationRepeatCount=0;
imgLoading.animationDuration=.5;
[imgLoading startAnimating];
在上面的代码中,我分别添加了图像,但是您可以循环填充它们。
- (CGImageRef)CGImageRotatedByAngle:(CGImageRef)imgRef angle:(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);
[(id)rotatedImage autorelease];
return rotatedImage;
}
上述功能可用于提供旋转的图像。
我认为您的方法是正确的;)您必须有一个容器对其进行修剪,以免看起来像孔框架在旋转。
UIImageView *imageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"image.png"]];
UIView *container = [UIView alloc] initWithFrame:CGRectMake(0,0,imageView.frame.size.height, image.frame.size.height)]; //i used the height because is the min(height, width).
[container setClipsToBounds:YES];
[container addSubview:imageView];
[imageView setCenter:CGPointMake(container.bounds.size.width/2, container.bounds.size.height/2)];
//animate your image here
...
也。 如果可能,请使用CALayer代替imageView;)
CALayer *imageLayer = [CALayer layer];
imageLayer.contents = (id)[UIImage imageNamed:@"myimage.png"].CGImage;
// and do basically the same as above,
[container.layer addSublayer:imageLayer];
//...
注意:上面的代码是经过大脑编译的,因此可能会有一些小错误,但是思路很清楚;)
希望能帮助到你
编辑:
嘿;)我想您忘了在视图中添加容器。 ;)
我刚刚尝试了代码,但我意识到容器的帧计算是不精确的(即使您不这样做也可以,但是动画看起来有点难看)这是代码:
UIImage *image = [UIImage imageNamed:@"sunrays.jpg"];
UIImageView *sunrays = [[UIImageView alloc] initWithImage:image];
float side = (MIN(image.size.height, image.size.width)/(2.0*sqrt(2)));
UIView *container = [[UIView alloc] initWithFrame:CGRectMake(0,0,side,side)];
[container setClipsToBounds:YES];
[container addSubview:sunrays];
[sunrays setCenter:CGPointMake(container.bounds.size.width/2, container.bounds.size.height/2)];
[self.view addSubview:container]; //you forgot this? ;)
[UIView beginAnimations:nil context:nil];
[UIView setAnimationDuration:5];
CABasicAnimation *rotationAnimation;
rotationAnimation = [CABasicAnimation animationWithKeyPath:@"transform.rotation.z"];
rotationAnimation.toValue = [NSNumber numberWithFloat:M_PI * 2.0 * 4.0];
rotationAnimation.duration = 1;
rotationAnimation.cumulative = YES;
rotationAnimation.repeatCount = 10;
rotationAnimation.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseOut];
[sunrays.layer addAnimation:rotationAnimation forKey:@"rotationAnimation"];
[UIView commitAnimations];
我知道这是很旧的帖子。 一种解决方案是使图像的黄色部分透明。 您可以使用免费工具pngweasel。 使黄色部分透明-这只会产生阳光的图像。 对于该图像使用纯黄色背景。 现在,您可以使用以下代码轻松旋转新的(阳光)图像
CABasicAnimation *rotate;
rotate = [CABasicAnimation animationWithKeyPath:@"transform.rotation"];
rotate.fromValue = [NSNumber numberWithFloat:0];
rotate.toValue = [NSNumber numberWithFloat:(0.0175*180)]; //[NSNumber numberWithFloat:deg2rad(10)];
rotate.duration = 0.5;
rotate.repeatCount = 1;
[self.image.layer addAnimation:rotate forKey:@"10"];
斯威夫特3:
var rotationAnimation = CABasicAnimation()
rotationAnimation = CABasicAnimation.init(keyPath: "transform.rotation.z")
rotationAnimation.toValue = NSNumber(value: (Double.pi))
rotationAnimation.duration = 1.0
rotationAnimation.isCumulative = true
rotationAnimation.repeatCount = 100.0
imageView.layer.add(rotationAnimation, forKey: "rotationAnimation")
这是UIView的扩展功能,可处理开始和停止旋转操作:
extension UIImageView {
func startRotation() {
let rotation : CABasicAnimation = CABasicAnimation(keyPath: "transform.rotation.z")
rotation.fromValue = 0
rotation.toValue = NSNumber(value: Double.pi)
rotation.duration = 5.0
rotation.isCumulative = true
rotation.repeatCount = FLT_MAX
self.layer.add(rotation, forKey: "rotationAnimation")
}
func stopRotation() {
self.layer.removeAnimation(forKey: "rotationAnimation")
}
}
现在使用UIView.animation闭包:
UIView.animate(withDuration: 0.5, animations: {
imageView.transform = CGAffineTransform(rotationAngle: (CGFloat(Double.pi))
}) { (isAnimationComplete) in
// Animation completed
}
使用扩展名查看结果:
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.