【iOS】Quartz2D信纸条纹

本文介绍了一种在iOS应用中使用小图片平铺作为背景的方法,并演示了如何利用此方法制作信纸条纹和电子书阅读器背景。通过简单的代码实现复杂的视觉效果。

 

一、前导程序

新建一个项目,在主控制器文件中实现以下几行代码,就能轻松的完成图片在视图中的平铺。

1 - (void)viewDidLoad
2 {
3     [super viewDidLoad];
4 
5     UIImage *image=[UIImage imageNamed:@"me"];
6     UIColor *color=[UIColor colorWithPatternImage:image];
7     self.view.backgroundColor=color;
8 }
View Code

效果:

 

二、实现信纸条纹的效果

利用上面的这种特性来做一个信纸的效果。
默认的view上没有分割线,要在view上加上分割线有两种方式:
(1)让美工做一张专门用来做背景的图片,把图片设置为背景。缺点:信的长度不确定,所以背景图片的长度也难以确定。
(2)通过一张小的图片来创建一个颜色,平铺实现背景效果。
 
第一步:生成一张以后用以平铺的小图片。
画矩形。
画线条。
第二步:从上下文中取出图片设置为背景。黑乎乎一片?(其他地方时透明的,控制器的颜色,如果不设置那么默认为黑色的)
实现代码:
 1 - (void)viewDidLoad
 2 {
 3     [super viewDidLoad];
 4 
 5     
 6     // 1.生成一张以后用于平铺的小图片
 7     CGSize size = CGSizeMake(self.view.frame.size.width, 35);
 8     UIGraphicsBeginImageContextWithOptions(size , NO, 0);
 9     
10     // 2.画矩形
11     CGContextRef ctx = UIGraphicsGetCurrentContext();
12     CGFloat height = 35;
13     CGContextAddRect(ctx, CGRectMake(0, 0, self.view.frame.size.width, height));
14     [[UIColor whiteColor] set];
15     CGContextFillPath(ctx);
16     
17     // 3.画线条
18     
19     CGFloat lineWidth = 2;
20     CGFloat lineY = height - lineWidth;
21     CGFloat lineX = 0;
22     CGContextMoveToPoint(ctx, lineX, lineY);
23     CGContextAddLineToPoint(ctx, 320, lineY);
24     [[UIColor blackColor] set];
25     CGContextStrokePath(ctx);
26     
27     
28     UIImage *image=UIGraphicsGetImageFromCurrentImageContext();
29     UIColor *color=[UIColor colorWithPatternImage:image];
30     self.view.backgroundColor=color;
31 }
View Code

效果:

三、应用场景

完成一个简陋的电子书阅读器

代码:

 1 - (void)viewDidLoad
 2 {
 3     [super viewDidLoad];
 4 
 5     
 6     // 1.生成一张以后用于平铺的小图片
 7     CGSize size = CGSizeMake(self.view.frame.size.width, 26);
 8     UIGraphicsBeginImageContextWithOptions(size , NO, 0);
 9     
10     // 2.画矩形
11     CGContextRef ctx = UIGraphicsGetCurrentContext();
12     CGFloat height = 26;
13     CGContextAddRect(ctx, CGRectMake(0, 0, self.view.frame.size.width, height));
14     [[UIColor brownColor] set];
15     CGContextFillPath(ctx);
16     
17     // 3.画线条
18     
19     CGFloat lineWidth = 2;
20     CGFloat lineY = height - lineWidth;
21     CGFloat lineX = 0;
22     CGContextMoveToPoint(ctx, lineX, lineY);
23     CGContextAddLineToPoint(ctx, 320, lineY);
24     [[UIColor blackColor] set];
25     CGContextStrokePath(ctx);
26     
27     
28     UIImage *image=UIGraphicsGetImageFromCurrentImageContext();
29     UIColor *color=[UIColor colorWithPatternImage:image];
30     //self.view.backgroundColor=color;
31     self.textview.backgroundColor=color;
32 }
33 
34 - (IBAction)perBtnClick:(UIButton *)sender {
35     self.index--;
36     self.textview.text=[NSString stringWithFormat:@"第%d页",self.index];
37     CATransition *ca = [[CATransition alloc] init];
38     ca.type = @"pageCurl";
39     
40     [self.textview.layer addAnimation:ca forKey:nil];
41     
42 }
43 
44 - (IBAction)nextBtnClick:(UIButton *)sender {
45     self.index++;
46     self.textview.text=[NSString stringWithFormat:@"第%d页",self.index];
47     CATransition *ca = [[CATransition alloc] init];
48     ca.type = @"pageCurl";
49     
50     [self.textview.layer addAnimation:ca forKey:nil];
51 }
View Code

 

storyboard中的界面布局

实现的简单效果:

       

 

转载于:https://www.cnblogs.com/surge/p/4184021.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值