码源第三方库UI

iOS自定义alertview

2015-02-12  本文已影响2473人  HarriesChen

在家闲来无事,于是就看起来ios绘图的那块,写点什么好呢?

鼓捣了一会,总算写出了一个小东西

这个是写完以后的效果

imageimage imageimage imageimage

这里我实现了三种款式的alertview
分别是成功,错误和警告,剩下的呢有空继续添加吧。

废话不说了,讲一下代码的思路

我用的是UIBezierPath进行绘图

ios有很多种绘图的方式,要讲的话在写几篇都写不完,这里就不详细介绍了。

UIBezierPath绘图分三个步骤

1.创建UIBezierPath路径
2.创建CAShapeLayer
3.将layer附加到图层

2和3之间还可以添加动画


[_logoView removeFromSuperview];
    _logoView = [[UIView alloc] initWithFrame:CGRectMake(([self getSelfSize].width-Simble_SIZE)/2, Simble_TOP, Simble_SIZE, Simble_SIZE)];
    
    UIBezierPath *path = [UIBezierPath bezierPathWithArcCenter:CGPointMake(Simble_SIZE/2, Simble_SIZE/2) radius:Simble_SIZE/2 startAngle:0 endAngle:M_PI*2 clockwise:YES];
    path.lineCapStyle = kCGLineCapRound;
    path.lineJoinStyle = kCGLineCapRound;
    
    [path moveToPoint:CGPointMake(Simble_SIZE/2, Simble_SIZE/6)];
    CGPoint p1 = CGPointMake(Simble_SIZE/2, Simble_SIZE/6*3.8);
    [path addLineToPoint:p1];
    
    [path moveToPoint:CGPointMake(Simble_SIZE/2, Simble_SIZE/6*4.5)];
    [path addArcWithCenter:CGPointMake(Simble_SIZE/2, Simble_SIZE/6*4.5) radius:2 startAngle:0 endAngle:M_PI*2 clockwise:YES];
    
    
    CAShapeLayer *layer = [[CAShapeLayer alloc] init];
    layer.fillColor = [UIColor clearColor].CGColor;
    layer.strokeColor = [UIColor orangeColor].CGColor;
    layer.lineWidth = 5;
    layer.path = path.CGPath;
    
    CABasicAnimation *animation = [CABasicAnimation animationWithKeyPath:NSStringFromSelector(@selector(strokeEnd))];
    animation.fromValue = @0;
    animation.toValue = @1;
    animation.duration = 0.5;
    [layer addAnimation:animation forKey:NSStringFromSelector(@selector(strokeEnd))];
    
    [_logoView.layer addSublayer:layer];
    
    [self addSubview:_logoView];

这个代码段是用来绘制一个叹号

GitHub代码在这

上一篇下一篇

猜你喜欢

热点阅读