Animation动画

本文详细介绍了一个自定义的iOS动画效果实现方案,通过Objective-C代码展示了如何创建并控制UIView动画,包括移动和缩放效果,以及按钮触发动画的交互设计。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

版权声明:本文为延成原创文章,转载请标明出处

#import "VCAnimation.h"

@interface VCAnimation ()
{
    NSArray* _titleArray;
    UIImageView* _imageView;
}
@end

@implementation VCAnimation

- (void)viewDidLoad {
    [super viewDidLoad];
    [self initView];
}

-(void)initView{
    
    _imageView = [[UIImageView alloc]initWithImage:[UIImage imageNamed:@"heart1"]];
    [_imageView setFrame:CGRectMake(180, 50, 50, 50)];
    [self.view addSubview:_imageView];
    
    _titleArray = [NSArray arrayWithObjects:@"移动",@"缩放", nil];
       for (int i=0; i<_titleArray.count; i++) {
           UIButton* btn = [UIButton buttonWithType:UIButtonTypeRoundedRect];
           [btn setFrame:CGRectMake(150,500+ i*60, 120, 40)];
           [btn setTitle:_titleArray[i] forState:UIControlStateNormal];
           [btn setBackgroundColor:[UIColor systemBlueColor]];
           [btn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
           [btn addTarget:self action:@selector(pressBtn:) forControlEvents:UIControlEventTouchUpInside];
           [btn setTag:101+i];
           [self.view addSubview:btn];
       }
}

-(void)pressBtn:(UIButton*)btn{
    switch (btn.tag) {
           case 101:{
               [UIView beginAnimations:nil context:nil];
               [UIView setAnimationDuration:2];
               [UIView setAnimationDelay:1];
               [UIView setAnimationDelegate:self];
               //设置动画轨迹方式
               //UIViewAnimationCurveEaseIn 开始时较慢
               //UIViewAnimationCurveEaseOut 结束时较慢
               //UIViewAnimationCurveEaseInOut 开始和结束时较慢
               //UIViewAnimationCurveLinear 整个过程匀速进行
               [UIView setAnimationCurve:UIViewAnimationCurveLinear];
               [UIView setAnimationDidStopSelector:@selector(endAnim)];
               [_imageView setFrame:CGRectMake(180, 600, 50, 50)];
               [UIView commitAnimations];
           }
               break;
           case 102:{
               [UIView beginAnimations:nil context:nil];
               [UIView setAnimationDuration:2];
               [UIView setAnimationDelegate:self];
               [UIView setAnimationDidStopSelector:@selector(endAnim)];
               [_imageView setFrame:CGRectMake(180, 50, 200, 200)];
               [_imageView setAlpha:0.1];
               [UIView commitAnimations];
           }
               break;
           default:
               break;
       }
}

-(void)endAnim{
    NSLog(@"动画结束");
}

@end
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值