iOS伸縮變化

通過CAKeyframeAnimation類實現視圖的伸縮變換。

#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;//動畫無限循環
    
    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:^{
        //通過相應的key移除對應layer上的Animation
        [self.testView.layer removeAnimationForKey:kAnimationKey];
    } completion:^(BOOL finished) {
        
    }];
}

@end

沒有封裝,直接實現效果,有需要用的自己加工一下。

最后編輯于
?著作權歸作者所有,轉載或內容合作請聯系作者
平臺聲明:文章內容(如有圖片或視頻亦包括在內)由作者上傳并發布,文章內容僅代表作者本人觀點,簡書系信息發布平臺,僅提供信息存儲服務。

推薦閱讀更多精彩內容