UI基础 - 训练营:利用Quartz2D模仿UIimageView显示图片

利用 Quartz2D 显示图片

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

2 - 代码示例:模仿 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 // 完成重绘
 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 // 自定义绘制:需重写 setter 方法,在方法中完成重绘,实现图片变更
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    // UIImageView
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
复制代码

运行效果

素材下载:图片素材

https://pan.baidu.com/s/1eoAvLP4zKVpiWwoaYGTlAQ

69n4

posted on   低头捡石頭  阅读(21)  评论(0编辑  收藏  举报

相关博文:
阅读排行:
· 全程不用写代码,我用AI程序员写了一个飞机大战
· DeepSeek 开源周回顾「GitHub 热点速览」
· 记一次.NET内存居高不下排查解决与启示
· MongoDB 8.0这个新功能碉堡了,比商业数据库还牛
· .NET10 - 预览版1新功能体验(一)
历史上的今天:
2018-03-19 UI定制 - 实现UITableViewCell的展开、闭合功能
< 2025年3月 >
23 24 25 26 27 28 1
2 3 4 5 6 7 8
9 10 11 12 13 14 15
16 17 18 19 20 21 22
23 24 25 26 27 28 29
30 31 1 2 3 4 5

导航

统计

点击右上角即可分享
微信分享提示