码迷,mamicode.com
首页 > 其他好文 > 详细

重写UIImageView的image属性---

时间:2014-07-10 11:48:25      阅读:309      评论:0      收藏:0      [点我收藏+]

标签:style   blog   http   color   2014   for   

 

效果:

 

bubuko.com,布布扣

 

当你重写了UIImageView的image属性后你就会对UIImageView怎么显示图片了如指掌了:

源码:

UIImageView.h  +  UIImageView.m

bubuko.com,布布扣
//
//  LiveImageView.h
//  Progress
//
//  Copyright (c) 2014年 L.S. All rights reserved.
//

#import <UIKit/UIKit.h>

@interface LiveImageView : UIImageView

@property (nonatomic, assign)  CGFloat  duration;

@end
bubuko.com,布布扣
bubuko.com,布布扣
//
//  LiveImageView.m
//  Progress
//
//  Copyright (c) 2014年 L.S. All rights reserved.
//

#import "LiveImageView.h"

@interface LiveImageView ()

{
    CALayer  *_Layer;
}

@end

@implementation LiveImageView

- (id)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self)
    {
        _duration = 0.3f;
        _Layer = self.layer;
    }
    return self;
}

// 重写image的setter与getter方法
@synthesize image = _image;

- (void)setImage:(UIImage *)image
{
    if (_image != image)
    {
        CABasicAnimation *ani = [CABasicAnimation animationWithKeyPath:@"contents"];
        ani.fromValue = (__bridge id)(_image.CGImage);
        ani.toValue   =  (__bridge id)(image.CGImage);
        ani.duration  = _duration;
        _Layer.contents = (__bridge id)(image.CGImage);
        [_Layer addAnimation:ani forKey:nil];
        
        _image = image;
    }
}

- (UIImage *)image
{
    return _image;
}

@end
bubuko.com,布布扣

以下是核心代码:

bubuko.com,布布扣

系统的setter方法绝对是这么写的哦:),亲自测试,因为是backed layer,赋值都是没有显式动画的.

bubuko.com,布布扣

 

//  LRootViewController.m
//  UIImageTest//  Copyright (c) 2014年 L.S. All rights reserved.
//

#import "LRootViewController.h"
#import "LiveImageView.h"

@interface LRootViewController ()
{
    NSArray *_imageArray;
    LiveImageView *liveView;
     __block int count;
}
@end

@implementation LRootViewController

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];

    _imageArray=[NSArray arrayWithObjects:[UIImage imageNamed:@"33.jpg"],[UIImage imageNamed:@"44.jpg"],[UIImage imageNamed:@"66.jpg"],nil];
    
    liveView=[[LiveImageView alloc]initWithFrame:CGRectMake(0, 0, 200, 200)];
    [self.view addSubview:liveView];
    liveView.center=self.view.center;
    NSTimer *timer=[NSTimer scheduledTimerWithTimeInterval:1.5 target:self selector:@selector(changeImage) userInfo:nil repeats:YES];
    [timer fire];
    
}

-(void)changeImage
{
   
    //改变图片
    [UIView animateWithDuration:0.3f animations:^{
        liveView.duration=0.3f;
        liveView.image=_imageArray[count++%3];//count++%4这是循环取数组中的对象
        //改变尺寸的效果
        CGRect tmpRect=liveView.bounds;
        tmpRect.size = liveView.image.size;
        liveView.bounds=tmpRect;
        liveView.center=self.view.center;
        
    }];

}

 

以下地方是改变尺寸的动画代码

bubuko.com,布布扣

 

 

 

 

重写UIImageView的image属性---,布布扣,bubuko.com

重写UIImageView的image属性---

标签:style   blog   http   color   2014   for   

原文地址:http://www.cnblogs.com/lisaloveyou1900/p/3812047.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!