美文网首页
Core Graphics & Core Animation

Core Graphics & Core Animation

作者: MoneyRobber | 来源:发表于2020-10-24 21:14 被阅读0次

架构图示


Core Graphics

// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
    
    //划线
    CGContextRef context = UIGraphicsGetCurrentContext();//获取当前图形上下文
    CGContextSetFillColorWithColor(context, [UIColor redColor].CGColor);
    CGContextFillRect(context, self.bounds);
    CGContextSetStrokeColorWithColor(context, [UIColor whiteColor].CGColor);
    CGContextMoveToPoint(context, 40, 40);
    CGContextAddLineToPoint(context, 100, 100);
    CGContextAddLineToPoint(context, 80, 120);
    CGContextAddLineToPoint(context, 130, 150);
    CGContextStrokePath(context);
    
    CGContextSetStrokeColorWithColor(context, [UIColor blackColor].CGColor);
    CGContextMoveToPoint(context, 130, 150);
    CGContextAddLineToPoint(context, 130, 200);
    CGContextStrokePath(context);
    
    //画矩形
    CGContextAddRect(context, CGRectMake(60, 60, 100, 100));
    CGContextStrokePath(context);
    
    //画圆弧
    CGContextAddArc(context, self.frame.size.width/2+80, self.frame.size.height/2-30, 50, 0, -2*M_PI, 1);
    CGContextSetFillColorWithColor(context, [UIColor yellowColor].CGColor);
    CGContextFillPath(context);
    CGContextStrokePath(context);
    
    //画椭圆
    CGContextAddEllipseInRect(context, CGRectMake(20, 230, 200, 50));
    //阴影
    CGContextSetShadow(context, CGSizeMake(0, 3), 2);
    //渐变色填充
    CGContextClip(context);
    CGColorSpaceRef colorspace = CGColorSpaceCreateDeviceRGB();
    CGFloat location[2] = {0.0, 1.0};
    CGFloat components[8] = {0.0, 1.0, 0.0, 1.0,1.0, 1.0, 0.0, 1.0};
    CGGradientRef gradient = CGGradientCreateWithColorComponents(colorspace, components, location, 2);
    CGContextDrawLinearGradient(context, gradient, CGPointMake(0, 0), CGPointMake(320, 0), kCGGradientDrawsBeforeStartLocation);
    CGContextStrokePath(context);
}

Core Animation

//CABasicAnimation
    CABasicAnimation *ani = [CABasicAnimation animationWithKeyPath:@"opacity"];
    ani.fromValue = [NSNumber numberWithFloat:1];
    ani.toValue = [NSNumber numberWithFloat:0];
    ani.duration = 2;
    customView.layer.opacity = 0;
    [customView.layer addAnimation:ani forKey:nil];
    [self.view addSubview:customView];
//摇动
CAKeyframeAnimation *rotateAnimation2 = [CAKeyframeAnimation animationWithKeyPath:@"transform.rotation.z"];
rotateAnimation2.values = @[
                                            [NSNumber numberWithFloat:(0)],
                                            [NSNumber numberWithFloat:(M_PI/8)],
                                            [NSNumber numberWithFloat:(-M_PI/8)],
                                            [NSNumber numberWithFloat:(M_PI/15)],
                                            [NSNumber numberWithFloat:(-M_PI/15)],
                                            [NSNumber numberWithFloat:(0)]];
                
rotateAnimation2.keyTimes = @[
                                              [NSNumber numberWithFloat:0],
                                              [NSNumber numberWithFloat:0.125],
                                              [NSNumber numberWithFloat:0.375],
                                              [NSNumber numberWithFloat:0.625],
                                              [NSNumber numberWithFloat:0.875],
                                              [NSNumber numberWithFloat:1]];
rotateAnimation2.duration = self.animationTimeOne;
rotateAnimation2.calculationMode = kCAAnimationCubic;
[self.danImageView.layer addAnimation:rotateAnimation2 forKey:nil];

相关文章

网友评论

      本文标题:Core Graphics & Core Animation

      本文链接:https://www.haomeiwen.com/subject/btlwmktx.html