旋轉:
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];