UI之imageView

UI编程实现图片动画播放

首先要在工程中添加几张你想要显示出来的图片,之后就可以开始写代码实现动画播放了

#import "MangoView.h"

#define kWidth  self.frame.size.width
#define kHeight self.frame.size.height
@implementation MangoView
//重写init方法,因为本类初始化时,如果子类重写了初始化方法就会执行子类初始化方法
- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {//自定义方法
        [self loadingCustomView];
    }
    return self;
}
- (void)loadingCustomView{
//    创建一个UIImage对象(照片)
    UIImage *image = [UIImage imageNamed:@"55.png"];
    //[NSBundle mainBundle]当前应用程序的主路径
    //pathForResource: ofType:在应用程序当前的主路径下边去寻找资源文件的名字的类型
    NSString *path = [[NSBundle mainBundle] pathForResource:@"7" ofType:@".png"];
    NSLog(@"path = %@",path);
    UIImage *image1 = [[UIImage alloc]initWithContentsOfFile:path];
    
    //创建一个UIImageView对象(相框)
    UIImageView *imageView = [[UIImageView alloc]initWithImage:image1];
    //设置imageView大小  image.size.width图片原来的宽 image.size.height图片原来的高
    //设置图片之后,如果不做任何默认设置图片会缩放到跟imageViewframe一样大小
    imageView.frame = CGRectMake(30, 30,image.size.width/2-180, image.size.height/2);
    //imageView.frame = self.frame;
    

    /*

   UIViewContentModeScaleAspectFit按图片原来的比例压缩

    UIViewContentModeScaleToFil压缩图片,是图片充满imageView,图片会变形
    按原来的比例在填充整个imageView,可能有些部分显示不出来
    UIViewContentModeTop 让图片从顶部开始显示
    UIViewContentModeBottom 让图片从底部开始显示

    UIViewContentModeTopLeft 让图片从顶部和左边开始显示

    */

    
    imageView.contentMode = UIViewContentModeScaleAspectFill;
    [self addSubview:imageView];
    [imageView release];
    
    //图片的用户交互默认是关闭状态,如果想让imageView以及上边的子视图响应用户交互,需要把imageView的userInteractionEnabled属性设置为YES
    NSLog(@"%d",imageView.userInteractionEnabled);
     //使用imageView播放一组图片的动画
    NSMutableArray *imageArray = [NSMutableArray array];
    for (int i = 1; i < 6; i++) {
      //%04d、%0xd 输出x位,如果不足x位的用0来补全
      // 通过for循环创建图片的名字
      NSString *imageString = [NSString stringWithFormat:@"%d.png",i];
      //通过图片名字创建UIImage对象
      UIImage *image = [UIImage imageNamed:imageString];
      //把图片对象添加到数组中
      [imageArray addObject:image];
    }
    //设置将要播放动画的图片数组
    imageView.animationImages = imageArray;
    //设置动画播放时间
    imageView.animationDuration = 2.0;
    //设置动画重复次数
    imageView.animationRepeatCount = 3;
     //播放
    [imageView startAnimating];

}

@end


#import "MangoViewController.h"
#import "MangoView.h"
@interface MangoViewController ()
@end
@implementation MangoViewController
- (void)loadView{
    [super loadView];
//    用自己创建的视图替换掉
    self.view = [[MangoView alloc]initWithFrame:self.view.frame];
}
- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值