iOS—实现UI imageview的底层
2015-09-09 20:53
309 查看
新建一个类 HM imageView 类似于系统自带的UI imageview 效果是一样的
// // HMUIimageView.h // 模仿imageView的底层实现 // // Created by YaguangZhu on 15/9/9. // Copyright (c) 2015年 YaguangZhu. All rights reserved. // #import <UIKit/UIKit.h> @interface HMUIimageView : UIView @property (nonatomic,strong)UIImage *image; @end // // HMUIimageView.m // 模仿imageView的底层实现 // // Created by YaguangZhu on 15/9/9. // Copyright (c) 2015年 YaguangZhu. All rights reserved. // #import "HMUIimageView.h" @implementation HMUIimageView // Only override drawRect: if you perform custom drawing. // An empty implementation adversely affects performance during animation. - (void)drawRect:(CGRect)rect { // Drawing code [_image drawInRect:rect]; } - (void)setImage:(UIImage *)image { _image = image; [self setNeedsDisplay]; } @end
// // ViewController.m // 模仿imageView的底层实现 // // Created by YaguangZhu on 15/9/9. // Copyright (c) 2015年 YaguangZhu. All rights reserved. // #import "ViewController.h" #import "HMUIimageView.h" @interface ViewController () { HMUIimageView *_imgV; } @end @implementation ViewController - (void)viewDidLoad { [super viewDidLoad]; // Do any additional setup after loading the view, typically from a nib. // UIImageView *imgV = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 250, 250)]; // imgV.image = [UIImage imageNamed:@"liuyan"]; // [self.view addSubview:imgV]; HMUIimageView *imgV = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 250, 250)]; imgV.image = [UIImage imageNamed:@"liuyan"]; [self.view addSubview:imgV]; _imgV = imgV; } - (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event { _imgV.image = [UIImage imageNamed:@"teacher"]; } - (void)didReceiveMemoryWarning { [super didReceiveMemoryWarning]; // Dispose of any resources that can be recreated. } @end
相关文章推荐
- Expression Tree Build
- 09-09 Service(续)、UI主线程、AsyncTask(UI主线程)
- Android Service——在子线程中更新UI
- iOS小结(五) 结合 Instrument 分析并解决memory issues
- interview questions
- ios-UIKit 练习
- UINavigationController和UIBarButtonItem
- UIView
- Qt5 中使用了QQuickWidget,程序退出时容易崩溃的解决办法。
- button初始化类目
- Java- 字符串(String,StringBuffer,StringBuilder类)
- iOSInterviewQuestions/iOS面试问题
- UIImagePickerController
- UIAlertController
- queue POJ 2259 Team Queue
- hdu5014Number Sequence 位运算
- 2-LTE Identification I: UE and ME Identifiers
- UGUI Button控件
- Sequential Monte Carlo Methods
- requestDisallowInterceptTouchEvent的用法