iOS 旋转和平移动画

旋转:

CABasicAnimation *tAnimation =  [CABasicAnimation animationWithKeyPath:@"transform.rotation.z"];

//默认是顺时针效果,若将fromValue和toValue的值互换,则为逆时针效果

tAnimation.fromValue = [NSNumber numberWithFloat:0.f];

tAnimation.toValue =  [NSNumber numberWithFloat: M_PI *2];

//旋转速度,数字越大旋转越慢

tAnimation.duration  = 10;

tAnimation.autoreverses = NO;

tAnimation.fillMode =kCAFillModeForwards;

tAnimation.repeatCount = MAXFLOAT; //如果这里想设置成一直自旋转,可以设置为MAXFLOAT,否则设置具体的数值则代表执行多少次

[self.flowerView.layer addAnimation:tAnimation forKey:nil];


平移,有两种方法,1是用定时器和animateWithDuration,2是用CABasicAnimation

1,

HomeViewController.m

self.cloudsTimer = [NSTimer scheduledTimerWithTimeInterval:5 target:self selector:@selector(moveCloudsAnimation) userInfo:nil repeats:YES];

- (void)moveCloudsAnimation {

 [self.cloudsView moveSelfView];

}

CloudsView.m

- (void)moveSelfView {

CGPoint point = self.center;

self.center = CGPointMake(-self.frame.size.width*0.5,point.y);

[UIView animateWithDuration:5 animations:^{

self.center = CGPointMake(Screen_Width+self.frame.size.width/2, point.y);

}];

}


2.和旋转一样,将key换成position即可

HomeViewController.m

CABasicAnimation *pAnimation =  [CABasicAnimation animationWithKeyPath:@"position"];

//默认是顺时针效果,若将fromValue和toValue的值互换,则为逆时针效果

pAnimation.fromValue = [NSValue valueWithCGPoint:CGPointMake(-Screen_Width/2, CGRectGetMidY(self.cloudsView.frame))];

pAnimation.toValue =  [NSValue valueWithCGPoint:CGPointMake(Screen_Width*1.5, CGRectGetMidY(self.cloudsView.frame))];

//旋转速度,数字越大旋转越慢

pAnimation.duration  = 20;

pAnimation.autoreverses = NO;

pAnimation.fillMode =kCAFillModeForwards;

pAnimation.repeatCount = MAXFLOAT; //如果这里想设置成一直自旋转,可以设置为MAXFLOAT,否则设置具体的数值则代表执行多少次

[self.cloudsView.layer addAnimation:pAnimation forKey:nil];

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • Core Animation Core Animation,中文翻译为核心动画,它是一组非常强大的动画处理API,...
    45b645c5912e阅读 8,170评论 0 21
  • 在iOS中随处都可以看到绚丽的动画效果,实现这些动画的过程并不复杂,今天将带大家一窥ios动画全貌。在这里你可以看...
    每天刷两次牙阅读 12,713评论 6 30
  • 在iOS中随处都可以看到绚丽的动画效果,实现这些动画的过程并不复杂,今天将带大家一窥iOS动画全貌。在这里你可以看...
    F麦子阅读 10,523评论 5 13
  • iOS动画篇之CoreAnimation动画 9月 22, 2016发布在Objective-C App如果想被大...
    白水灬煮一切阅读 6,507评论 0 0
  • 我们好像在哪见过,你记得吗?你的名字是什么,我不记得了,虽然我曾努力的去记住。 曾经,我是你,你是我,在不同时空的...
    会画画的云阅读 4,214评论 0 17