其他分享
首页 > 其他分享> > UI绘图 - Quartz2D:训练营 02(模仿UIimageView | )

UI绘图 - Quartz2D:训练营 02(模仿UIimageView | )

作者:互联网

1 - Quartz2D 最大的用途在于自定义 View,当系统的 View 不能满足我们使用需求的时候就需要自己绘制 View

2 - 代码示例:使用 Quartz2D 自定义 View,模仿系统的 ImageView

// - QuartsView.h

1 #import <UIKit/UIKit.h>
2 @interface QuartsView : UIView
3 
4 @property(nonatomic,strong)UIImage *image;// 图片
5 
6 @end

// - QuartsView.m

 1 #import "QuartsView.h"
 2 @implementation QuartsView
 3 
 4 - (void)drawRect:(CGRect)rect{
 5     [self.image drawInRect:rect];
 6 }
 7 
 8 // 重写 set 方法完成重绘
 9  -(void)setImage:(UIImage *)image{
10      
11     _image = image;
12     [self setNeedsDisplay];
13  }
14 
15 @end

// - ViewController.m

 1 #import "ViewController.h"
 2 #import "QuartsView.h"
 3 #define SCREEN_WIDTH   [UIScreen mainScreen].bounds.size.width
 4 #define SCREENH_HEIGHT [UIScreen mainScreen].bounds.size.height
 5 @interface ViewController ()
 6 @property(nonatomic,strong)UIImageView *imageView;
 7 @property(nonatomic,strong)QuartsView  *customedView;
 8 @end
 9 @implementation ViewController
10 
11 - (void)viewDidLoad{
12     [super viewDidLoad];
13     self.navigationController.navigationBar.hidden = YES;
14     
15     [self testUIImageView];
16     [self testCustomedView];
17 
18 }
19 
20 // UIimageView:使用系统控件,可直接变更图片
21 -(void)testUIImageView{
22     
23     self.imageView = [[UIImageView alloc] init];
24     self.imageView.image = [UIImage imageNamed:@"11.png"];
25     self.imageView.frame=CGRectMake((SCREEN_WIDTH-160)/2.0, 80, 160, 100);
26     [self.view addSubview:self.imageView];
27     
28     UIButton *btn = [[UIButton alloc]initWithFrame:CGRectMake((SCREEN_WIDTH-100)/2.0, 200, 100, 50)];
29     [btn setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
30     btn.tag = 101;
31     [btn setTitle:@"点击切换" forState:UIControlStateNormal];
32     [btn addTarget:self action:@selector(btnClick:) forControlEvents:UIControlEventTouchUpInside];
33     [self.view addSubview:btn];
34     
35 }
36 
37 
38 // 自定义绘制:需要重写 set方法,在此方法中完成重绘
39 -(void)testCustomedView{
40     
41     self.customedView = [[QuartsView alloc] init];
42     self.customedView.image = [UIImage imageNamed:@"11.png"];
43     self.customedView.frame = CGRectMake((SCREEN_WIDTH-160)/2.0, 350, 160, 100);
44     [self.view addSubview:self.customedView];
45     
46     UIButton *btn = [[UIButton alloc]initWithFrame:CGRectMake((SCREEN_WIDTH-100)/2.0, 470, 100, 50)];
47     [btn setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
48     btn.tag = 102;
49     [btn setTitle:@"点击切换" forState:UIControlStateNormal];
50     [btn addTarget:self action:@selector(btnClick:) forControlEvents:UIControlEventTouchUpInside];
51     [self.view addSubview:btn];
52     
53 }
54 
55 
56 -(void)btnClick:(UIButton*)sender{
57 
58     if (sender.tag == 101) {
59         if ([self.imageView.image isEqual:[UIImage imageNamed:@"11.png"]]) {
60             UIImage *image = [UIImage imageNamed:@"33.png"];
61             self.imageView.image = image;
62             return;
63         }
64         UIImage *image = [UIImage imageNamed:@"11.png"];
65         self.imageView.image = image;
66         return;
67     }
68     
69     if ([self.customedView.image isEqual:[UIImage imageNamed:@"11.png"]]) {
70         UIImage *image = [UIImage imageNamed:@"33.png"];
71         self.customedView.image = image;
72         return;
73     }
74     UIImage *image = [UIImage imageNamed:@"11.png"];
75     self.customedView.image = image;
76 
77 }
78 
79 @end

运行效果

 

标签:02,self,UIimageView,customedView,UI,imageView,UIImage,btn,image
来源: https://www.cnblogs.com/self-epoch/p/16026276.html