我有一个阳光图像。我喜欢把它放在背景上,让它以慢动作连续旋转。
图像就是这个..
我尝试用CABasicAnimation旋转它,但它会旋转整个画面..我只想让阳光旋转在背景而不是整个画面..
有什么办法吗?
更新
这就是我在做什么......请指出我的错误...... :(
我没有得到你已解释过的.. :(
这就是我在做什么...
- (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];
}
...更新
我做了你说的nacho ...感谢你的回复,但是得到了一个空白...... :(
我编码了......
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];
代码有什么问题?
答案 0 :(得分:3)
嗯,我认为你应该生成不同的图像然后尝试动画这个
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;
}
以上功能可用于提供旋转的图像..
答案 1 :(得分:1)
我认为你的方法是正确的;)你必须有一个容器来夹它,所以它看起来不像孔框架在旋转。
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];
答案 2 :(得分:0)
我知道这是很老的帖子。 一种解决方案是使图像透明为黄色部分。您可以使用免费工具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 :(得分:0)
斯威夫特3:
library(rvest)
library(dplyr)
words<-read_html("https://www.education.com/magazine/article/Ed_50_Words_SAT_Loves_2/")
just_words<-words %>% html_nodes("ol") %>% html_text()
strsplit(just_words,"\r\n\t")
这是UIView的扩展函数,用于处理start&amp;停止旋转操作:
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.animation闭包:
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")
}
}
使用扩展程序查看结果: