iOS贝塞尔曲线画哆啦A梦的代码实例

2020-01-20 23:57:39刘景俊

7、画右手部分


  //右手
  CAShapeLayer *rightHandLayer = [CAShapeLayer layer];
  UIBezierPath *rightHandPath = [UIBezierPath bezierPathWithArcCenter:CGPointMake(arcCenterX+85*cos(27/180.0*M_PI), arcCenterY-85*sin(27/180.0*M_PI)) radius:20 startAngle:0 endAngle:2*M_PI clockwise:YES];
  [self setLayer:rightHandLayer path:rightHandPath ];


  //右胳膊
  CAShapeLayer *rightArmLayer = [CAShapeLayer layer];
  UIBezierPath *rightArmPath = [UIBezierPath bezierPath];
  [rightArmPath moveToPoint:CGPointMake(arcCenterX+80*cos(13/180.0*M_PI), arcCenterY-80*sin(13/180.0*M_PI))];
  [rightArmPath addQuadCurveToPoint:CGPointMake(arcCenterX+distanceXToArcCenter, arcCenterY+distanceYToArcCenter) controlPoint:CGPointMake(arcCenterX+80*cos(13/180.0*M_PI)+9, arcCenterY+20)];
  [rightArmPath addLineToPoint:CGPointMake(arcCenterX+distanceXToArcCenter, arcCenterY+distanceYToArcCenter+25)];
  [rightArmPath addQuadCurveToPoint:CGPointMake(arcCenterX+93*cos(15/180.0*M_PI), arcCenterY-93*sin(15/180.0*M_PI)) controlPoint:CGPointMake(arcCenterX+90*cos(13/180.0*M_PI)+15, arcCenterY+25)];
  [rightArmPath addQuadCurveToPoint:CGPointMake(arcCenterX+80*cos(13/180.0*M_PI), arcCenterY-80*sin(13/180.0*M_PI)) controlPoint:CGPointMake(arcCenterX+80*cos(13/180.0*M_PI)+5, arcCenterY-93*sin(15/180.0*M_PI)+5)];
  [self setLayer:rightArmLayer path:rightArmPath];

  UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(self.view.frame.size.width/2+90, 120, 70, 30)];
  label.textAlignment = NSTextAlignmentCenter;
  label.textColor = [UIColor colorWithRed:21/255.0 green:159/255.0 blue:237/255.0 alpha:1];
  label.text = @"Hello";
  label.font = [UIFont fontWithName:@"Chalkduster" size:20];
  [self.view addSubview:label];

  CABasicAnimation *animation = [CABasicAnimation animationWithKeyPath:@"transform.scale"];
  animation.fromValue = @(0);
  animation.toValue = @(1);
  animation.duration = 0.5f;
  [label.layer addAnimation:animation forKey:nil];

8、给各个部位上颜色


 //上色
  faceLayer.fillColor = [UIColor whiteColor].CGColor;
  leftEyeLayer.fillColor = [UIColor whiteColor].CGColor;
  rightEyeLayer.fillColor = [UIColor whiteColor].CGColor;
  leftEyeBallLayer.fillColor = [UIColor blackColor].CGColor;
  rightEyeBallLayer.fillColor = [UIColor blackColor].CGColor;
  noseLayer.fillColor = [UIColor redColor].CGColor;
  noseHaloLayer.fillColor = [UIColor whiteColor].CGColor;
  headLayer.fillColor = [UIColor colorWithRed:21/255.0 green:159/255.0 blue:237/255.0 alpha:1].CGColor;
  leftArmLayer.fillColor = [UIColor colorWithRed:21/255.0 green:159/255.0 blue:237/255.0 alpha:1].CGColor;
  leftHandLayer.fillColor = [UIColor whiteColor].CGColor;
  mufflerLayer.fillColor = [UIColor redColor].CGColor;
  bellyLayer.fillColor = [UIColor whiteColor].CGColor;
  bellLayer.fillColor = [UIColor yellowColor].CGColor;
  bodyLayer.fillColor = [UIColor colorWithRed:21/255.0 green:159/255.0 blue:237/255.0 alpha:1].CGColor;
  rightHandLayer .fillColor=[UIColor whiteColor].CGColor ;
  rightArmLayer .fillColor=[UIColor colorWithRed:21/255.0 green:159/255.0 blue:237/255.0 alpha:1] .CGColor;