点线模式(2)

上面的控制器中用到了一个自定义控件类:FKDashLineView,当用户通过UISlider调整phase或通过按钮重设phase时,程序都会修改FKDashLineView的dashPhase属性,从而改变该自定义控件的绘制外观;当用户通过UIPickerView选择不同的列表项时,程序会调用FKDashLineView的setDashPattern:count:方法。

下面是FKDashLineView自定义类的接口代码。

程序清单:codes/12/12.2/DashLineTest/DashLineTest/FKDashLineView.h
 

 
 
  1. @interface FKDashLineView : UIView  
  2. {  
  3.     CGFloat dashPattern[10];  
  4.     size_t dashCount;  
  5. }  
  6. @property(nonatomic, assign) CGFloat dashPhase;  
  7. -(void)setDashPattern:(CGFloat*)pattern count:(size_t)count;  
  8. @end 

FKDashLineView的实现代码将会重写drawRect:方法来绘制该控件,绘制该控件时将会根据不同的dashPhase参数和dashPattern参数来选择使用不同的点线模式。下面是该自定义控件类的实现代码。

程序清单:codes/12/12.2/DashLineTest/DashLineTest/FKDashLineView.m
 

 
 
  1. @implementation FKDashLineView  
  2. @synthesize dashPhase;  
  3. -(id)initWithFrame:(CGRect)frame  
  4. {  
  5.     self = [super initWithFrame:frame];  
  6.     if(self != nil)  
  7.     {  
  8.         self.opaque = YES;  
  9.         self.backgroundColor = [UIColor blackColor];  
  10.         // 设置每次清空上一次绘制的内容  
  11.         self.clearsContextBeforeDrawing = YES;  
  12.         dashCount = 0;  
  13.         dashPhase = 0.0;  
  14.     }  
  15.     return self;  
  16. }  
  17. -(void)setDashPhase:(CGFloat)phase  
  18. {  
  19.     // 如果新传入的phase参数与原有dashPash参数不相等  
  20.     if(phase != dashPhase)  
  21.     {  
  22.         dashPhase = phase;  // 对dashPhase赋值  
  23.         [self setNeedsDisplay];  // 通知该控件重绘自己  
  24.     }  
  25. }  
  26. -(void)setDashPattern:(CGFloat *)pattern count:(size_t)count  
  27. {  
  28.     // 如果count与dashCount不相等,或者dashPattern数组与pattern数组不相等  
  29.     if((count != dashCount) || (memcmp(dashPattern, pattern  
  30.         , sizeof(CGFloat) * count) != 0))  
  31.     {  
  32.         // 将pattern数组的值复制到dashPattern数组  
  33.         memcpy(dashPattern, pattern, sizeof(CGFloat) * count);  
  34.         dashCount = count;  // 对dashCount赋值  
  35.         [self setNeedsDisplay];  // 通知该控件重绘自己  
  36.     }  
  37. }  
  38. // 重写该方法,绘制该控件  
  39. -(void)drawRect:(CGRect)rect  
  40. {     
  41.     CGContextRef ctx = UIGraphicsGetCurrentContext();  // 获取绘图上下文  
  42.     CGContextSetRGBStrokeColor(ctx, 1.0, 0.0, 1.0, 1.0);  // 设置线条颜色  
  43.     CGContextSetLineWidth(ctx, 2.0);  // 设置线宽  
  44.     // 设置点线模式  
  45.     CGContextSetLineDash(ctx, dashPhase, dashPattern, dashCount);  
  46.     CGPoint line1[] = {CGPointMake(10.0, 20.0), CGPointMake(310.0, 20.0)};    
  47.     CGContextStrokeLineSegments(ctx, line1, 2);  // 绘制一条线段  
  48.     CGPoint line2[] = {CGPointMake(160.0, 130.0), CGPointMake(160.0, 130.0)};  
  49.     CGContextStrokeLineSegments(ctx, line2, 2);  // 绘制一条线段    
  50.     CGContextStrokeRect(ctx, CGRectMake(10.0, 30.0, 100.0, 100.0));  // 绘制一个矩形  
  51.     CGContextStrokeEllipseInRect(ctx, CGRectMake(210.0, 30.0, 100.0, 100.0));   // 绘制一个椭圆  
  52. }  
  53. @end 

该程序的关键在于粗体字代码,这行粗体代码设置了绘制图形所用的点线模式,该点线模式的dashPhase、dashPattern都来自该控件的属性。这些属性会随着用户操作界面上的UISlider、UIPickerView改变,这样就可以使用不同的点线模式来绘制该UI控件上的线条了。

编译、运行该程序,可以看到如图12.4所示的结果。

当用户选择UIPickerView的不同列表项目时,可以看到界面上点线模式的实线长度、间距会随选择而动态改变;如果用户拖动界面上的UIPickerView,将会看到点线模式的phase参数不断改变,形成类似动画的效果。这是因为程序采用不同的phase绘制点线时,就会形成线条在流动的错觉。
 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值