06-模仿系统的UIImageView
2015-12-09 22:03
435 查看
06-模仿系统的UIImageView
整体思路: 我们想要模仿系统的UIImageView,我们必须得要知道系统的UIView怎么用. 系统的用法是创建一个UIImageView对象,设置frame,给它传递一个UIImage,再把它添加到一个View上面就可以了. 可以切换图片. 这是第一个用法. 第二种用法,就是在创建的时候直接传递一个UIImage对象,使用initWithImage的方法进行创建一个UImageView的方式 用这种做法创建出来的UIImageView它的尺寸大小和原始图片的尺寸大小一样大. 所以我们自己的UIImageView也要具有这些功能. 实现步骤: 第一步:新建一个UIView,起名ZYQImageView. 第二步:给ZYQImageView添加一个UIImage属性,供外界传递图片
第三步:在DrawRect方法当中把传递的图片绘制到View上面
绘制方法为:[_image drawInRect:rect],绘制的图片尺寸大小和UIView的尺寸大小一样大.
第四步:重写UIImage属性的set方法,在set方法当中让View重新绘制.目的为了能够办到切换图片.
第五步:提供一个- (instancetype)initWithImage:(UIImage *)image方法.
在这个方法当中重写init方法
在初始化时,让View尺寸和图片的实际大小一样大.
然后再给UIImage属性赋值.
这样在绘制图片的时候,显示出来的View已经有尺寸了, 尺寸大小和图片的实际大小一样大.
具体代码实现:
- (instancetype)initWithImage:(UIImage *)image{
if (self = [super init]) {
self.frame = CGRectMake(0, 0, image.size.width, image.size.height);
_image = image;
}
return self;
}
-(void)setImage:(UIImage *)image{
_image = image;
[self setNeedsDisplay];
}
- (void)drawRect:(CGRect)rect {
[_image drawInRect:rect];
}
相关文章推荐
- 05-UIKit绘图演练
- java.sql.SQLException: Value'0000-00-00'异常解决办法
- A Guide For Troubleshooting Workflow Notification Emails - Inbound and Outbound [ID 831982.1]
- mySQL中删除unique key的语法
- 让UIImageView适应图片的大小
- iOS 关于UIImage的压缩问题
- Android 笔记:AndroidTrain , Lint , build.gradle , 第三方库
- ubuntu12.04+ros fuerte 跑LsdSlam代码
- iOS开发UI篇—以微博界面为例使用纯代码自定义cell程序编码全过程(三·完结)
- abdroid hwui
- Putty和Xming创建远程GUI的SSH连接
- android120 zhihuibeijing 开机页面
- ERROR (DiscoveryFailure): Could not determine a suitable URL for the plugin
- nsq源码阅读笔记之nsqd(三)——diskQueue
- UIImageView的图片居中问题
- UITextView使用
- leetcode Longest Consecutive Sequence
- Error:ERROR ITMS-90474: "Invalid Bundle. iPad Multitasking support requires these orientations:
- Could not execute JDBC batch update : java.sql.BatchUpdateException: ORA-00926: 缺失 VALUES 关键字
- UE4连接Oculuse DK2设备