QuartzCore
CAGradientLayer
UIView *tempView = [[UIView alloc] initWithFrame:CGRectMake(50, 50, 300, 300)];
[self.view addSubview:tempView];
CAGradientLayer *gradientLayer = [CAGradientLayer layer];
gradientLayer.frame = tempView.bounds;
gradientLayer.colors = @[(__bridge id)[UIColor redColor].CGColor, (__bridge id)[UIColor whiteColor].CGColor, (__bridge id)[UIColor orangeColor].CGColor];
gradientLayer.locations = @[@(0.3), @(0.5), @(0.8)];
gradientLayer.startPoint = CGPointMake(0, 0);
gradientLayer.endPoint = CGPointMake(1, 1);
[tempView.layer addSublayer:gradientLayer];
CAShapeLayer
CAShapeLayer *shapeLayer = [CAShapeLayer layer];
UIBezierPath *path = [UIBezierPath bezierPath];
[path moveToPoint:CGPointMake(100, 100)];
[path addCurveToPoint:CGPointMake(200, 200) controlPoint1:CGPointMake(150, 75) controlPoint2:CGPointMake(150, 175)];
shapeLayer.path = path.CGPath;
shapeLayer.lineWidth = 2.f;
shapeLayer.strokeColor = [UIColor blueColor].CGColor;
shapeLayer.fillColor = [UIColor whiteColor].CGColor;
[tempView.layer addSublayer:shapeLayer];
CAEmitterLayer
CAEmitterLayer *emitterLayer = [CAEmitterLayer layer];
emitterLayer.backgroundColor = [UIColor whiteColor].CGColor;
// 发射位置
emitterLayer.emitterPosition = CGPointMake(150, 50);
// 发射源尺寸
emitterLayer.emitterSize = CGSizeMake(100, 10);
// 发射源模式
emitterLayer.emitterMode = kCAEmitterLayerSurface;
// 发射形状
emitterLayer.emitterShape = kCAEmitterLayerCircle;
NSMutableArray *mArr = [NSMutableArray array];
for (NSInteger i = 1; i < 5; i++) {
@autoreleasepool {
CAEmitterCell *cell = [CAEmitterCell emitterCell];
cell.name = @"name";
cell.birthRate = 10.f;
cell.lifetime = 15.f;
// 运动速度
cell.velocity = 1.0f;
// 运动速度的浮动值
cell.velocityRange = 10;
// y方向的加速度
cell.yAcceleration = 2;
// 抛洒角度
cell.emissionRange = 0.5 * M_PI;
// 自旋转角度的范围
cell.spinRange = 0.25 * M_PI;
// 粒子透明度改变速度
cell.alphaSpeed = 2.f;
UIImage *cellImg = [UIImage imageNamed:@"cell"];
cell.contents = (id)cellImg.CGImage;
[mArr addObject:cell];
}
}
emitterLayer.emitterCells = mArr;
[tempView.layer addSublayer:emitterLayer];
CAAnimation
CABasicAnimation *startAnimation = [CABasicAnimation animationWithKeyPath:@"strokeStart"];
startAnimation.fromValue = @(0);
startAnimation.toValue = @(1);
startAnimation.duration = 3.f;
startAnimation.removedOnCompletion = NO;
startAnimation.fillMode = kCAFillModeForwards;
[shapeLayer addAnimation:startAnimation forKey:@"strokeStart"];
CAAnimationGroup
CABasicAnimation *startAnimation = [CABasicAnimation animationWithKeyPath:@"strokeStart"];
startAnimation.fromValue = @(0);
startAnimation.toValue = @(1);
CABasicAnimation *colorAnimation = [CABasicAnimation animationWithKeyPath:@"strokeColor"];
colorAnimation.fromValue = (__bridge id)[UIColor blueColor].CGColor;
colorAnimation.toValue = (__bridge id)[UIColor redColor].CGColor;
CAAnimationGroup *group = [CAAnimationGroup animation];
group.animations = @[startAnimation, colorAnimation];
group.duration = 3.f;
group.removedOnCompletion = NO;
group.fillMode = kCAFillModeForwards;
[shapeLayer addAnimation:group forKey:@"group"];
CATransition
UIImageView *imgView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"transition1"]];
imgView.frame = CGRectMake(50, 350, 100, 100);
[self.view addSubview:imgView];
CATransition *transition = [CATransition animation];
transition.type = kCATransitionMoveIn;
transition.subtype = kCATransitionFromRight;
transition.duration = 5;
transition.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseInEaseOut];
[imgView.layer addAnimation:transition forKey:@"transition"];
CAKeyframeAnimation
CAKeyframeAnimation *keyframeAnimation = [CAKeyframeAnimation animationWithKeyPath:@"strokeColor"];
keyframeAnimation.values = @[(__bridge id)[UIColor redColor].CGColor, (__bridge id)[UIColor blueColor].CGColor, (__bridge id)[UIColor orangeColor].CGColor];
keyframeAnimation.duration = 5.0;
keyframeAnimation.removedOnCompletion = NO;
keyframeAnimation.fillMode = kCAFillModeForwards;
[shapeLayer addAnimation:keyframeAnimation forKey:@"keyframeAnimation"];
CoreGraphics
绘制三角形
CGContextRef context = UIGraphicsGetCurrentContext();
CGContextMoveToPoint(context, 100, 100);
CGContextAddLineToPoint(context, 150, 150);
CGContextAddLineToPoint(context, 100, 150);
CGContextSetLineWidth(context, 2);
CGContextSetStrokeColorWithColor(context, [UIColor redColor].CGColor);
CGContextClosePath(context);
CGContextStrokePath(context);
运行结果:
CGContextRef context = UIGraphicsGetCurrentContext();
CGPoint points[3];
points[0] = CGPointMake(100, 100);
points[1] = CGPointMake(150, 150);
points[2] = CGPointMake(100, 150);
CGContextSetStrokeColorWithColor(context, [UIColor redColor].CGColor);
CGContextSetLineWidth(context, 2.f);
CGContextAddLines(context, points, 3);
CGContextClosePath(context);
CGContextDrawPath(context, kCGPathStroke);
绘制扇形
CGContextRef context = UIGraphicsGetCurrentContext();
CGContextMoveToPoint(context, 80, 80);
CGContextAddArc(context, 80, 80, 60, 0, 0.5 * M_PI, YES);
CGContextSetStrokeColorWithColor(context, [UIColor redColor].CGColor);
CGContextSetLineWidth(context, 2);
CGContextClosePath(context);
CGContextStrokePath(context);
运行结果:
CGContextRef context = UIGraphicsGetCurrentContext();
CGContextMoveToPoint(context, 80, 80);
CGContextAddArc(context, 80, 80, 80, 0, 2 * M_PI / 3, NO);
CGContextSetFillColorWithColor(context, [UIColor redColor].CGColor);
CGContextFillPath(context);
运行结果:
绘制圆形
CGContextRef context = UIGraphicsGetCurrentContext();
CGContextMoveToPoint(context, 80, 80);
CGContextAddArc(context, 80, 80, 80, 0, 2 * M_PI, NO);
CGContextSetFillColorWithColor(context, [UIColor redColor].CGColor);
CGContextFillPath(context);
运行结果:
贝塞尔曲线
三次贝塞尔曲线
CGContextRef context = UIGraphicsGetCurrentContext();
CGContextMoveToPoint(context, 100, 100);
CGContextAddCurveToPoint(context, 10, 20, 130, 150, 10, 180);
CGContextSetStrokeColorWithColor(context, [UIColor redColor].CGColor);
CGContextSetLineWidth(context, 2);
CGContextClosePath(context);
CGContextStrokePath(context);
运行结果:
二次贝塞尔曲线
CGContextRef context = UIGraphicsGetCurrentContext();
CGContextMoveToPoint(context, 100, 100);
CGContextAddQuadCurveToPoint(context, 100, 150, 150, 100);
CGContextSetStrokeColorWithColor(context, [UIColor redColor].CGColor);
CGContextSetLineWidth(context, 2.f);
CGContextClosePath(context);
CGContextStrokePath(context);
运行结果:
字符串
NSDictionary *attrDict = @{NSForegroundColorAttributeName: [UIColor redColor], NSBackgroundColorAttributeName: [UIColor yellowColor], NSFontAttributeName: [UIFont systemFontOfSize:16]};
[@"ArkMu" drawInRect:CGRectMake(100, 100, 100, 30) withAttributes:attrDict];
运行结果:
图片
UIImage *image = [UIImage imageNamed:@"sky.jpg"];
[image drawAtPoint:CGPointMake(0, 0)];
运行结果: