用杯赛尔曲线(做动画和绘图)

1. 用被塞尔曲线做动画
效果:位置沿着贝瑟尔曲线位置移动,尺寸由大到小,透明度从完全可见过渡到彻底透明。
至于在DrawRect里面绘制贝塞尔曲线,可以直接百度,没有什么难点的。
 
- (void)clickButton:(id)sender
{
  UIBezierPath *path = [UIBezierPath bezierPath];
  [path moveToPoint:btn.center];
  [path addCurveToPoint:btn.center
  controlPoint1:CGPointMake(20, 300)
  controlPoint2:CGPointMake(300, 460)];
 
 
  //位置
  CAKeyframeAnimation *animation = [CAKeyframeAnimation animationWithKeyPath:@"position"];
  animation.path = path.CGPath;
 
  //缩放
  CABasicAnimation *scaleAnim = [CABasicAnimation animationWithKeyPath:@"transform"];
  scaleAnim.fromValue = [NSValue valueWithCATransform3D:CATransform3DIdentity];
  scaleAnim.toValue = [NSValue valueWithCATransform3D:CATransform3DMakeScale(0.1, 0.1, 1.0)];
 
  //透明度
  CABasicAnimation *alphaAnimation = [CABasicAnimation animationWithKeyPath:@"opacity"];//不是用alpha
  alphaAnimation.fromValue=[NSNumber numberWithFloat:1.0];
  alphaAnimation.toValue=[NSNumber numberWithFloat:0.1];
 
  //动画组
  CAAnimationGroup *groups = [CAAnimationGroup animation];
  groups.animations = [NSArray arrayWithObjects:animation, alphaAnimation, scaleAnim,nil];
  groups.duration = 5.0f;
  groups.removedOnCompletion = NO;
  groups.fillMode = kCAFillModeForwards;
  groups.delegate = self;
  [groups setValue:@"groupsAnimation" forKey:@"animationName"];
  [btn.layer addAnimation:groups forKey:@"poaasgdsition scale"];
}
 
//这个方法竟然是来自NSObject
- (void)animationDidStop:(CAAnimation *)anim finished:(BOOL)flag
{
    NSLog(@"Finished");
}

  

2. 贝塞尔曲线绘图(三次) copy from http://www.henishuo.com/uibezierpath-draw/

在UIView类的DrawRect方法中

- (void)drawThirdBezierPath {

  UIBezierPath *path = [UIBezierPath bezierPath];
  
  // 设置起始端点
  [path moveToPoint:CGPointMake(20, 150)];
  
  [path addCurveToPoint:CGPointMake(300, 150)
          controlPoint1:CGPointMake(160, 0)
          controlPoint2:CGPointMake(160, 250)];
  
  path.lineCapStyle = kCGLineCapRound;
  path.lineJoinStyle = kCGLineJoinRound;
  path.lineWidth = 5.0;
  
  UIColor *strokeColor = [UIColor redColor];
  [strokeColor set];
  
  [path stroke];
}

  

顺便show一下自定义UIView的大致结构

参考IOS7 Programming Cookbook ch20

#import <UIKit/UIKit.h>
@interface TestView : UIView
@end

@implementation TestView

- (id)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
    [self setBackgroundColor:[UIColor clearColor]];
    }
    return self;
}

-(void)setImage2:(UImage *img)
{
   [self setneesDisplay];
}

// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect
{
  //draw string1
  NSString *str = @"abc";
  UIFont *font = [UIFont fontWithName:@"HelveticaNeue-Bold" size:10.0f];
  [str drawAtPoint:CGPointMake(0,0)
    withAttributes:@{
    NSFontAttributeName:font,
    NSForegroundColorAttributeName:[UIColor redColor]
    }];

  //draw string2
  NSString *str2 = @"abc2";
  UIFont *font2 = [UIFont fontWithName:@"HelveticaNeue-Bold" size:12.0f];
  [str2 drawAtPoint:CGPointMake(40,0)
    withAttributes:@{
    NSFontAttributeName:font2,
    NSForegroundColorAttributeName:[UIColor greenColor]
    }];

  //draw image1
  UIImage *img = [UIImage imageNamed:@"voice.png"];
  [img drawAtPoint:CGPointMake(0, 0)];

  //draw image2
  [img drawInRect:CGRectMake(80, 80, 20, 10)];

  //draw Line
  [[UIColor brownColor] set];

  CGContextRef currentContext = UIGraphicsGetCurrentContext();
  CGContextSetLineWidth(currentContext, 4.0f);
  CGContextSetLineJoin(currentContext, kCGLineJoinRound);//接缝处
  CGContextMoveToPoint(currentContext, 20, 20);
  CGContextAddLineToPoint(currentContext, 50, 80);
  CGContextAddLineToPoint(currentContext, 100, 10);
  CGContextStrokePath(currentContext);
}
@end

  

原文地址:https://www.cnblogs.com/dongfangchun/p/5350503.html