我想知道如何制作一个UIView动画,首先缩放尺寸并旋转该尺寸而不是原始尺寸旋转?
我尝试了很多方法,比如在完成UIView动画时包装UIView动画,使用UIView animateKeyframes等等。
但是,我无法完美地构建动画,请给我提示或关键字进行搜索,谢谢!
答案 0 :(得分:3)
这应该可以根据您的要求工作(如何制作首先缩放尺寸并旋转该尺寸而不是在原始尺寸上旋转的UIView动画?)
@IBOutlet var scaleRotateImage: UIImageView!
func scaleNTransform() -> Void {
scaleRotateImage.layer.cornerRadius = scaleRotateImage.frame.size.height/2.0
scaleRotateImage.clipsToBounds = true
let scaleTransform = CGAffineTransform(scaleX: 3.0, y: 3.0) // Scale
let rotateTransform = CGAffineTransform(rotationAngle: CGFloat.pi) // Rotation
let hybridTransform = scaleTransform.concatenating(rotateTransform) // Both Scale + Rotation
// Outer block of animation
UIView.animate(withDuration: 5.0, animations: {
self.scaleRotateImage.transform = scaleTransform
self.view.layoutIfNeeded()
}) { (isCompleted) in
// Nested block of animation
UIView.animate(withDuration: 5.0, animations: {
self.scaleRotateImage.transform = hybridTransform
self.view.layoutIfNeeded()
})
}
}
的结果强>
答案 1 :(得分:2)
你可以做点什么,
定义弧度以将度数转换为弧度
#define RADIANS(degrees) (((degrees) * M_PI) / 180.0)
然后在你的viewDidAppear
,
-(void)viewDidAppear:(BOOL)animated{
[super viewDidAppear:animated];
CGAffineTransform leftTransform = CGAffineTransformRotate(CGAffineTransformIdentity, RADIANS(-10.0));
CGAffineTransform rightTransform = CGAffineTransformRotate(CGAffineTransformIdentity, RADIANS(10.0));
_myView.transform = leftTransform; //Here `_myView` is the your view on which you want animations!
[UIView beginAnimations:@"youCanSetAnimationIdHere" context:(__bridge void * _Nullable)(_myView)];
[UIView setAnimationRepeatAutoreverses:YES];
[UIView setAnimationRepeatCount:5];
[UIView setAnimationDuration:0.25];
[UIView setAnimationDelegate:self];
[UIView setAnimationDidStopSelector:@selector(animationCompletedWithAnimationId:isCompleted:view:)];
_myView.transform = rightTransform;
[UIView commitAnimations];
}
并且您的animationCompletedWithAnimationId
方法应该是,
- (void) animationCompletedWithAnimationId:(NSString *)animationID isCompleted:(NSNumber *)isCompleted view:(UIView *)view
{
if ([isCompleted boolValue]) {
UIView *myView = view;
myView.transform = CGAffineTransformIdentity;
}
}
结果是
您可以根据自己的要求更改重复次数和弧度!
答案 2 :(得分:0)
希望您已完成缩放或摇动UIButton
,此代码将指导您重复UIView
动画序列重复和自动反转。
btn.frame = frame1;
[UIView animateKeyframesWithDuration:2.0 delay:0.0 options:UIViewKeyframeAnimationOptionAutoreverse | UIViewKeyframeAnimationOptionRepeat animations:^{
[UIView addKeyframeWithRelativeStartTime:0.0 relativeDuration:0.5 animations:^{
// Your changes to the UI, eg. scale UIButton up 300 points
btn.frame = frame2;
}];
[UIView addKeyframeWithRelativeStartTime:0.5 relativeDuration:0.5 animations:^{
// Your changes to the UI, eg. rotate or shake UIButton
}];
} completion:nil];