UIImageView 实现简单动画

本文介绍了如何在Objective-C中为UIImageView添加动画效果,包括设置动画图像数组、动画时长和重复次数,并通过按钮控制动画的开始和停止。

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

今天看到项目里面代码,有些UIImageView的动画,所以动手写了下
1.UIImageView一些可能不常用的与动画有关的属性

    @property(nonatomic,copy) NSArray *animationImages;            // The array must contain UIImages. Setting hides the single image. default is nil
    @property(nonatomic,copy) NSArray *highlightedAnimationImages NS_AVAILABLE_IOS(3_0);            // The array must contain UIImages. Setting hides the single image. default is nil
    
    @property(nonatomic) NSTimeInterval animationDuration;         // for one cycle of images. default is number of images * 1/30th of a second (i.e. 30 fps)
    @property(nonatomic) NSInteger      animationRepeatCount;      // 0 means infinite (default is 0)
    
    - (void)startAnimating;
    - (void)stopAnimating;
    - (BOOL)isAnimating;


2.代码实现:有两个button控制 start,stop

//1.
    UIImageView *imageView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 20, 20)];
    self.animationImageView = imageView;
    animationImageView.animationImages = [NSArray arrayWithObjects:
                                          [UIImage imageNamed:@"1.png"],
                                          [UIImage imageNamed:@"2.png"],
                                          [UIImage imageNamed:@"3.png"],
                                          [UIImage imageNamed:@"4.png"],
                                          [UIImage imageNamed:@"5.png"],
                                          nil];
    animationImageView.animationDuration = 1.5;
    animationImageView.animationRepeatCount = 0;
    [self.view addSubview:animationImageView];
    [imageView release];
    
    UIButton *startAnimationButton = [UIButton buttonWithType:UIButtonTypeRoundedRect];
    [startAnimationButton setFrame:CGRectMake(100, 200, 50, 50)];
    [startAnimationButton setTitle:@"start" forState:UIControlStateNormal];
    [startAnimationButton addTarget:self action:@selector(startAnimationImageView) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:startAnimationButton];
    
    UIButton *endAnimationButton = [UIButton buttonWithType:UIButtonTypeRoundedRect];
    [endAnimationButton setFrame:CGRectMake(100, 300, 50, 50)];
    [endAnimationButton setTitle:@"stop" forState:UIControlStateNormal];
    [endAnimationButton addTarget:self action:@selector(endAnimationImageView) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:endAnimationButton];

- (void)startAnimationImageView
{
    [self.animationImageView startAnimating];
}

- (void)endAnimationImageView
{
    [self.animationImageView stopAnimating];
}



//参考:http://blog.youkuaiyun.com/maojudong/article/details/6898430



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值