iOS伸缩变化

作者: 追沐 | 来源:发表于2017-07-29 14:32 被阅读7次

通过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

没有封装,直接实现效果,有需要用的自己加工一下。

相关文章

  • iOS伸缩变化

    通过CAKeyframeAnimation类实现视图的伸缩变换。 没有封装,直接实现效果,有需要用的自己加工一下。

  • iOS图片伸缩技巧

    纵观移动市场,一款移动app,要想长期在移动市场立足,最起码要包含以下几个要素:实用的功能、极强的用户体验、华丽简...

  • 0224 如何面对高并发?缓存?中台为什么会火?

    场景的变化 场景特点变化点老复杂的业务逻辑新大量用户高并发访问软件开发方法,过程管理,组织架构变化 垂直伸缩 提高...

  • 人生的“伸缩缝”

    在建筑学上,有一个特殊的名词“伸缩缝”,意谓建筑物体必须留一个伸缩的空间,以防空气的冷热变化时,结构体收缩膨...

  • iOS 图片拉伸缩放

    今天给大家分享一个表格头视图图片自由拉伸和缩放(类似于微信的朋友圈,供有需要的小伙伴使用) 在网上找到的第三方 在...

  • React Native从入门到深入三、FlexBox布局

    一、FlexBox布局 1.1 FlexBox是什么意思呢? flexible(形容词):能够伸缩或者很容易变化,...

  • Flex布局在React Native中的运用

    一、FlexBox布局 1.1 FlexBox是什么意思呢? flexible(形容词):能够伸缩或者很容易变化,...

  • iOS Scrollview 的头部view的拉伸伸缩效

    iOS Scrollview 的头部view的拉伸伸缩效 往左边滑动拉伸原理相同! 1、 先说拉伸放大的实现原理 ...

  • 红木家具的自我保护系统--伸缩缝

    红木家具的自我保护系统--伸缩缝 伸缩缝,是中国古典家具的一种传统加工工艺,即为了家具部件随季节气候的变化而正常伸...

  • 变形缝的主要分类2022-07-10

    变形缝可分为伸缩缝、沉降缝、防震缝三种。 伸缩缝:建筑构件因温度和湿度等因素的变化会产生胀缩变形。为此,通常在建筑...

网友评论

    本文标题:iOS伸缩变化

    本文链接:https://www.haomeiwen.com/subject/fjfclxtx.html