通過CAKeyframeAnimation類實現(xiàn)視圖的伸縮變換译蒂。
#import "ViewController.h"
#define kWidth [UIScreen mainScreen].bounds.size.width
#define kHeight [UIScreen mainScreen].bounds.size.height
#define kAnimationKey @"AnimationKey"
static const NSUInteger kStartButtonTag = 1000;
static const NSUInteger kStopButtonTag = 1001;
@interface ViewController ()
@property (nonatomic, strong) UIView *testView;
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
[self setUpUI];
}
- (void)setUpUI {
_testView = [[UIView alloc] initWithFrame:CGRectMake(kWidth * 0.5 - 50, kHeight * 0.5 - 50, 100, 100)];
_testView.backgroundColor = [[UIColor blackColor]colorWithAlphaComponent:.1];
_testView.layer.cornerRadius = _testView.frame.size.width * 0.5;
[self.view addSubview:self.testView];
UIButton *startButton = [UIButton buttonWithType:UIButtonTypeCustom];
startButton.frame = CGRectMake(kWidth*0.5-50, kHeight-200, 100, 60);
[startButton setTitle:@"開啟動畫" forState:UIControlStateNormal];
[startButton setTitleColor:[UIColor redColor] forState:UIControlStateNormal];
[startButton setTag:kStartButtonTag];
[startButton addTarget:self action:@selector(buttonAction:) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:startButton];
UIButton *stopSender = [UIButton buttonWithType:UIButtonTypeCustom];
stopSender.frame = CGRectMake(kWidth*0.5-50, kHeight-100, 100, 60);
[stopSender setTitle:@"停止動畫" forState:UIControlStateNormal];
[stopSender setTitleColor:[UIColor redColor] forState:UIControlStateNormal];
[stopSender setTag:kStopButtonTag];
[stopSender addTarget:self action:@selector(buttonAction:) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:stopSender];
}
- (void)buttonAction:(UIButton *)sender {
switch (sender.tag) {
case kStartButtonTag:
//開啟動畫
[self showAnimation];
break;
case kStopButtonTag:
//移除動畫
[self removeAnimation];
break;
}
}
//添加動畫方法
- (void)showAnimation {
CAKeyframeAnimation *animation = [CAKeyframeAnimation animationWithKeyPath:@"transform"];
animation.duration = 1.5;//動畫時間
animation.repeatCount = HUGE;//動畫無限循環(huán)
NSMutableArray *values = [NSMutableArray array];
[values addObject:[NSValue valueWithCATransform3D:CATransform3DMakeScale(1, 1, 1.0)]];
[values addObject:[NSValue valueWithCATransform3D:CATransform3DMakeScale(1.5, 1.5, 1.0)]];
[values addObject:[NSValue valueWithCATransform3D:CATransform3DMakeScale(1.0, 1.0, 1.0)]];
animation.values = values;
[self.testView.layer addAnimation:animation forKey:kAnimationKey];
}
//移除動畫方法
- (void)removeAnimation {
//此處duration最好與動畫時長保持一致
[UIView animateWithDuration:1.5 animations:^{
//通過相應(yīng)的key移除對應(yīng)layer上的Animation
[self.testView.layer removeAnimationForKey:kAnimationKey];
} completion:^(BOOL finished) {
}];
}
@end
沒有封裝被饿,直接實現(xiàn)效果彩掐,有需要用的自己加工一下医咨。