UI基础 - Quartz2D 05:信纸 | 圆的缩放 | 刷帧

信纸

1 - 实现信纸条纹的效果,实际上就是把所要绘制的图形放到 Bitmap 上

复制代码
 1 #import "ViewController.h"
 2 #define SCREEN_WIDTH   [UIScreen mainScreen].bounds.size.width
 3 #define SCREENH_HEIGHT [UIScreen mainScreen].bounds.size.height
 4 @implementation ViewController
 5 
 6 - (void)viewDidLoad{
 7     [super viewDidLoad];
 8     self.navigationController.navigationBar.hidden = YES;
 9 
10     CGFloat height = 35;
11     CGSize size = CGSizeMake(SCREEN_WIDTH, height);
12     
13     // C  方法:CGBitmapContextCreate
14     // OC 方法
15     // 方式一:UIGraphicsBeginImageContext
16     // 方法二:创建一个 Bitmap 上下文:生成一张用于平铺的小图片(第一个方法来创建的图片清晰度、质量都没有第二种方法好)
17     // BOOL:YES 代表透明,NO 代表不透明     GFloat scale:是否缩放,0 代表不缩放
18     UIGraphicsBeginImageContextWithOptions(size , NO, 0);// 创建出来的 bitmap 就对应一个 UIImage 对象
19     
20     // 把矩形、线条绘制在图片上
21     CGContextRef ctx = UIGraphicsGetCurrentContext();
22     CGContextAddRect(ctx, CGRectMake(0, 0, SCREEN_WIDTH, height));
23     [[UIColor cyanColor] set];
24     CGContextFillPath(ctx);
25     // 线条
26     CGFloat lineWidth = 1.5;
27     CGFloat lineY = height - lineWidth;
28     CGFloat lineX = 0;
29     // 从矩形底部画起
30     CGContextMoveToPoint(ctx, lineX, lineY);
31     CGContextAddLineToPoint(ctx, SCREEN_WIDTH, lineY);
32     [[UIColor blackColor] set];
33     CGContextStrokePath(ctx);// 渲染
34 
35     UIImage *image = UIGraphicsGetImageFromCurrentImageContext();// 获取生成的图片
36     UIColor *color = [UIColor colorWithPatternImage:image];// 平铺
37     UIGraphicsEndImageContext();
38     self.view.backgroundColor = color;
39     
40     
41     // 伪代码:如果想要生成的图片保存到本地:先将图片转换为二进制数据,然后再将图片写到文件中
42     // NSData *data = UIImagePNGRepresentation(image);
43     // [data writeToFile:@"你的路径" atomically:YES];
44 }
45 
46 @end
复制代码

运行效果

圆的缩放

1 - 使用 Slider 控制圆的缩放

// - QuartsView.h

1 #import <UIKit/UIKit.h>
2 @interface QuartsView : UIView
3 
4 // 提供一个属性来接收外界传入的半径
5 @property(nonatomic,assign)float radius;
6 
7 @end

// - QuartsView.m

复制代码
 1 #import "QuartsView.h"
 2 @implementation QuartsView
 3 
 4 // 在 set 方法中为半径赋值
 5 -(void)setRadius:(float)radius{
 6     
 7     _radius = radius;
 8     [self setNeedsDisplay];// 重绘
 9 }
10 
11 // 如果 view 是从 xib 或 storyboard 中创建出来的,会先调用 awakefromnib 方法
12 - (void)awakeFromNib{
13     [super awakeFromNib];
14     self.radius = 20;// 在这里为圆的半径设定一个初始的值
15 }
16 
17 - (void)drawRect:(CGRect)rect{
18   
19     CGContextRef ctx = UIGraphicsGetCurrentContext();
20     CGContextAddArc(ctx, 180, 200, self.radius, 0, 2*M_PI, 0);
21     [[UIColor redColor]set];
22     CGContextFillPath(ctx);
23 }
24 
25 
26 @end
复制代码

// - ViewController.m

复制代码
 1 #import "ViewController.h"
 2 #import "QuartsView.h"
 3 @implementation ViewController
 4 
 5 - (void)viewDidLoad{
 6     [super viewDidLoad];
 7     self.navigationController.navigationBar.hidden = YES;
 8     
 9      // 画布
10     QuartsView *QV = [[QuartsView alloc] initWithFrame:self.view.frame];
11     QV.backgroundColor = [UIColor cyanColor];
12     QV.tag = 200;
13     [self.view addSubview:QV];
14     QV.radius = 80;
15    
16     UISlider *slider = [[UISlider alloc] initWithFrame:CGRectMake(10, self.view.frame.size.width, self.view.frame.size.width - 20, 10)];
17     slider.minimumTrackTintColor = [UIColor redColor];
18     slider.maximumTrackTintColor = [UIColor greenColor];
19     slider.minimumValue = 0.1;
20     slider.maximumValue = 1.0;
21     slider.continuous = YES;
22     [self.view addSubview:slider];
23     [slider addTarget:self action:@selector(changeSlider:) forControlEvents:UIControlEventValueChanged];
24 }
25 
26 -(void)changeSlider:(UISlider *)sender{
27     
28     QuartsView *QV = (QuartsView *)[self.view viewWithTag:200];
29     QV.radius = sender.value * 100;
30 }
31 
32 @end
复制代码

运行效果:thumb 滑至中间

刷帧

1 - 把图片绘制到 view 上,实现该图片在视图中循环下落的效果

复制代码
 1 #import "QuartsView.h"
 2 @interface QuartsView  ()
 3 @property(nonatomic,assign)float imageY;
 4 
 5 @end
 6 @implementation QuartsView
 7 
 8 
 9 - (instancetype)initWithFrame:(CGRect)frame{
10     self = [super initWithFrame:frame];
11     if (self) {
12         
13         // 方式一:定时器出现卡顿的现象
14         //[NSTimer scheduledTimerWithTimeInterval:0.1 target:self selector:@selector(updateImage) userInfo:nil repeats:YES];
15 
16         
17         // 方式二
18         // CADisplayLink 刷帧:默认每秒刷新 60 次。该定时器创建之后默认是不会执行的,需要把它加载到消息循环中
19         CADisplayLink *display= [CADisplayLink displayLinkWithTarget:self selector:@selector(updateImage)];
20         [display addToRunLoop:[NSRunLoop mainRunLoop] forMode:NSDefaultRunLoopMode];
21     }
22     return self;
23 }
24 
25 -(void)updateImage{
26 
27     [self setNeedsDisplay];
28 }
29 
30 // 把图片绘制到 view 上
31 - (void)drawRect:(CGRect)rect{
32     
33     // 每次调用该方法对画面进行重绘时,imageY 的值就 +5
34     self.imageY += 5;
35     // 判断:当雪花超出屏幕的时候让图片从头开始降落
36     if (self.imageY > rect.size.height) {
37         self.imageY = 0;
38     }
39     
40     UIImage *image = [UIImage imageNamed:@"狗子.jpeg"];
41     [image drawAtPoint:CGPointMake(0, self.imageY)];
42 }
43 
44 @end
复制代码

 

posted on   低头捡石頭  阅读(49)  评论(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

导航

统计

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