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

你可能感兴趣的:(iOS 旋转和平移动画)