您的位置:首页 > 产品设计 > UI/UE

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

2015-08-31 21:39 441 查看
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
 
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: