UIday0603:UIImageView的属性和用法 Tom猫举例

UIImageView常⽤用属性

     
     image                    //设置图⽚
     animationImages   //设置⼀组动态图片
     animationDuration  //设置播放一次一组动态图片的时间
     animationRepeatCount   //设置重复次数
     startAnimating   //开始动画

     stopAnimating     //结束动画


UIImageView相当于一个相框,可以存放一个或一组图片  UIImage 是图片对象


AppDelegate.m

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
    self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
    // Override point for customization after application launch.
    self.window.backgroundColor = [UIColor whiteColor];
    [self.window makeKeyAndVisible];
    
    RootViewController * rootVC = [[RootViewController alloc]init];
    self.window.rootViewController = rootVC;
    
    return YES;
}

RootViewController.m

#import "RootViewController.h"

@interface RootViewController ()

@property(nonatomic,strong)RootView * rv;

@end

@implementation RootViewController

-(void)loadView{
    self.rv = [[RootView alloc]initWithFrame:[UIScreen mainScreen].bounds];
    self.view = _rv;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    // 设置初始背景图片
    self.rv.imageView.image = [UIImage imageNamed:@"cat_angry0000.jpg"];  
    
    // 添加eatButton点击事件
    [self.rv.eatButton addTarget:self action:@selector(eatButtonAction:) forControlEvents:UIControlEventTouchUpInside];
    
    
    // 添加knockOutButton:点击事件
    [self.rv.knockOutButton addTarget:self action:@selector(knockOutButton:) forControlEvents:UIControlEventTouchUpInside];
      
}

-(void)eatButtonAction:(UIButton *)sender{
    [self p_ImageViewAnimationWithName:@"cat_eat" count:40 duration:0.08];
}
-(void)knockOutButton:(UIButton *)sender{
    [self p_ImageViewAnimationWithName:@"cat_knockout" count:81 duration:0.08];
}

 //私有方法:播放
-(void)p_ImageViewAnimationWithName:(NSString *)name count:(NSInteger)count duration:(NSTimeInterval)time{
    
    if (![self.rv.imageView isAnimating] && ![name isEqualToString:@"cat_listen"]) {
        NSMutableArray * tempArray = [NSMutableArray array];
        // 拼接图片 并将图片名字放入数组
        for (int i=0 ; i<count; i++) {
            NSString * s = [NSString stringWithFormat:@"%@%04d.jpg",name,i];
            UIImage * image = [UIImage imageNamed:s];
            [tempArray addObject:image];
        }
        // 给animationImages 数组赋值
        self.rv.imageView.animationImages = tempArray;
        
        // 播放时间
        self.rv.imageView.animationDuration = count * time;
        
        // 播放次数
        self.rv.imageView.animationRepeatCount = 1;
        
        // 播放动画
        [self.rv.imageView startAnimating];
    }else{
        NSMutableArray * tempArray = [NSMutableArray array];
        // 拼接图片 并将图片名字放入数组
        NSString * s = [NSString stringWithFormat:@"cat_listen.jpg"];
        UIImage * image = [UIImage imageNamed:s];
        [tempArray addObject:image];
        
        // 给animationImages 数组赋值
        self.rv.imageView.animationImages = tempArray;
        
        // 播放时间
        self.rv.imageView.animationDuration = 1;
        
        // 播放次数
        self.rv.imageView.animationRepeatCount = 1;
        
        // 播放动画
        [self.rv.imageView startAnimating];
    }
}

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

@end

RootView.h

#import <UIKit/UIKit.h>

@interface RootView : UIView

@property(nonatomic,strong)UIImageView * imageView;
@property(nonatomic,strong)UIButton * eatButton;
@property(nonatomic,strong)UIButton * knockOutButton;

@end

RootView.m

#import "RootView.h"

@implementation RootView

- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        [self p_setupViews];
    }
    return self;
}

// 布局
-(void)p_setupViews{
    self.backgroundColor = [UIColor yellowColor];
    // 设置图片大小和RootView一样大
    self.imageView = [[UIImageView alloc]initWithFrame:self.bounds];
    self.imageView.backgroundColor = [UIColor grayColor];
    [self addSubview:_imageView];
    
    //添加上eatButton
    self.eatButton = [UIButton buttonWithType:UIButtonTypeSystem];
    self.eatButton.frame = CGRectMake(50, 50, 50, 50);
    self.eatButton.backgroundColor = [UIColor blueColor];
    [self addSubview:_eatButton];
    
    //
    self.knockOutButton = [UIButton buttonWithType:UIButtonTypeSystem];
    self.knockOutButton.frame = CGRectMake(100, 150, 180, 180);
    self.knockOutButton.backgroundColor = [UIColor clearColor];
    [self addSubview:_knockOutButton];
}
@end
 


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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值