六 、Quartz 2D 自定义view

Quartz 2D 用武之地,可能也就是自定义view了。

新建,KKImageView 继承 UIView

KKImageView.h

#import <UIKit/UIKit.h>

@interface KKImageView : UIView

/** img */
@property (nonatomic, strong) UIImage *image;

@end

KKImageView.m

#import "KKImageView.h"

@implementation KKImageView


// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    
    [_image drawInRect:rect];
    
    
}

- (void)setImage:(UIImage *)image{
    _image = image;
    [self setNeedsDisplay];
}

@end

 

在控制器中这样使用

#import "ViewController.h"
#import "KKImageView.h"

@interface ViewController ()
/** <#注释#> */
@property (nonatomic, weak) KKImageView *imgView;

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    /*
    UIImageView *imgView = [[UIImageView alloc] init];
    imgView.image = [UIImage imageNamed:@"001"];
    imgView.frame = CGRectMake(20, 20, 200, 350);
    [self.view addSubview:imgView];
    */
    
    KKImageView *imgView = [[KKImageView alloc] init];
    _imgView = imgView;
    imgView.image = [UIImage imageNamed:@"001"];
    imgView.frame = CGRectMake(20, 20, 200, 350);
    [self.view addSubview:imgView];
    
}
- (IBAction)btnClick:(UIButton *)sender {
    
    _imgView.image = [UIImage imageNamed:@"002"];
    
}

@end

 

posted @ 2016-04-11 09:39  人生路1/5  阅读(189)  评论(0编辑  收藏  举报