源码0308-模仿UIImageView

Posted laugh

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了源码0308-模仿UIImageView相关的知识,希望对你有一定的参考价值。

 

//  ViewController.m
//  08-模仿UIImageView
#import "ViewController.h"

#import "XMGImageView.h"

@interface ViewController ()

@property (nonatomic, strong) XMGImageView *imageV;

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
    
//    UIImageView *imageV = [[UIImageView alloc] initWithFrame:CGRectMake(50, 50, 200, 350)];
    
    // 默认控件的尺寸跟图片一样
//    UIImageView *imageV = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"汽水"]];
//    imageV.image = [UIImage imageNamed:@"汽水"];
    
    
//    _imageV = imageV;
    
    
    // 默认图片把控件填充,图片的尺寸跟控件尺寸一样大
//    [self.view addSubview:imageV];
    
//    XMGImageView *imageView = [[XMGImageView alloc] initWithFrame:CGRectMake(50, 50, 200, 350)];
//    _imageV = imageView;
   XMGImageView *imageView = [[XMGImageView alloc] initWithImage:[UIImage imageNamed:@"汽水"]];
//    imageView.image = [UIImage imageNamed:@"汽水"];
//    
    [self.view addSubview:imageView];
    
}

- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
    _imageV.image = [UIImage imageNamed:@"CTO"];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end

 

//  XMGImageView.h
//  08-模仿UIImageView

#import <UIKit/UIKit.h>

@interface XMGImageView : UIView

@property (nonatomic, strong) UIImage *image;
- (instancetype)initWithImage:(UIImage *)image;
@end
//  XMGImageView.m
//  08-模仿UIImageView
#import "XMGImageView.h"

@implementation XMGImageView

- (instancetype)initWithImage:(UIImage *)image
{
    // 默认跟图片尺寸一样大
    if (self = [super initWithFrame:CGRectMake(0, 0, image.size.width, image.size.height)]) {
        
        _image = image;
    }
    
    return self;
}

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

// 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];
}


@end

 

以上是关于源码0308-模仿UIImageView的主要内容,如果未能解决你的问题,请参考以下文章

源码0308-画板

iOS开发UI篇—Quartz2D(自定义UIImageView控件)

模仿 UICollectionView 图片预览

Android 逆向类加载器 ClassLoader ( 类加载器源码简介 | BaseDexClassLoader | DexClassLoader | PathClassLoader )(代码片段

模仿照片应用程序的平移行为(速度)

为啥 UIImageView 不旋转?