iOS 图形图像动画 Core Animation

//Core Animation

#define WeakSelf __weak __typeof(self) weakSelf = self
#define StrongSelf __strong __typeof(weakSelf) self = weakSelf //添加
- (void)add:(id)sender {
[UIView animateWithDuration: animations:^{
[UIView setAnimationTransition:UIViewAnimationTransitionCurlDown forView:self.view cache:YES];
[UIView setAnimationCurve:UIViewAnimationCurveEaseInOut];
}];
}
//翻页
- (void)curl:(id)sender { WeakSelf;
[UIView animateWithDuration: animations:^{
StrongSelf;
[UIView setAnimationTransition:UIViewAnimationTransitionCurlUp forView:self.view cache:YES];
[UIView setAnimationCurve:UIViewAnimationCurveEaseInOut];
[self.view exchangeSubviewAtIndex: withSubviewAtIndex:];
}];
}
//移入
- (void)move:(id)sender {
CATransition *trans = [CATransition animation];
trans.duration = 2.0f;
trans.type = kCATransitionMoveIn;
trans.subtype = kCATransitionFromLeft;
[self.view.layer addAnimation:trans forKey:@"animation"];
[self.view exchangeSubviewAtIndex: withSubviewAtIndex:];
}
//揭开
- (void)reveal:(id)sender {
CATransition *trans = [CATransition animation];
trans.duration = 2.0f;
trans.type = kCATransitionReveal;
trans.subtype = kCATransitionFromTop;
[self.view.layer addAnimation:trans forKey:@"animation"];
[self.view exchangeSubviewAtIndex: withSubviewAtIndex:];
}
//立方体
- (void)cube:(id)sender {
CATransition *trans = [CATransition animation];
trans.duration = 2.0f;
trans.type = @"cube";
trans.subtype = kCATransitionFromLeft;
[self.view.layer addAnimation:trans forKey:@"animation"];
[self.view exchangeSubviewAtIndex: withSubviewAtIndex:];
}
//收缩
- (void)suck:(id)sender {
CATransition *trans = [CATransition animation];
trans.duration = 2.0f;
trans.type = @"suckEffect";
[self.view.layer addAnimation:trans forKey:@"animation"];
[self.view exchangeSubviewAtIndex: withSubviewAtIndex:];
}
//翻转
- (void)oglFlip:(id)sender {
CATransition *trans = [CATransition animation];
trans.duration = 2.0f;
trans.type = @"oglFlip";
trans.subtype = kCATransitionFromBottom;
[self.view.layer addAnimation:trans forKey:@"animation"];
[self.view exchangeSubviewAtIndex: withSubviewAtIndex:];
}
//水波
- (void)ripple:(id)sender {
CATransition *trans = [CATransition animation];
trans.duration = 2.0f;
trans.type = @"rippleEffect";
[self.view.layer addAnimation:trans forKey:@"animation"];
[self.view exchangeSubviewAtIndex: withSubviewAtIndex:];
}

draw default shape

    CGContextRef ctx = UIGraphicsGetCurrentContext();
CGContextSetLineWidth(ctx, );
CGContextSetRGBStrokeColor(ctx, , , , ); //straight line
const CGPoint points1[] = {CGPointMake(, ),CGPointMake(, ),CGPointMake(, ),CGPointMake(, )};
CGContextStrokeLineSegments(ctx, points1, ); CGContextSetLineCap(ctx, kCGLineCapSquare);
const CGPoint points2[] = {CGPointMake(, ),CGPointMake(, ),CGPointMake(, ),CGPointMake(, )};
CGContextStrokeLineSegments(ctx, points2, ); CGContextSetLineCap(ctx, kCGLineCapRound);
const CGPoint points3[] = {CGPointMake(, ),CGPointMake(, ),CGPointMake(, ),CGPointMake(, )};
CGContextStrokeLineSegments(ctx, points3, ); //dashed line
CGContextSetLineCap(ctx, kCGLineCapButt);
CGContextSetLineWidth(ctx, );
CGFloat patterns1[] = {,};
CGContextSetLineDash(ctx, , patterns1, ); const CGPoint points4[] = {CGPointMake(, ),CGPointMake(, )};
CGContextStrokeLineSegments(ctx, points4, ); CGContextSetLineDash(ctx, , patterns1, );
const CGPoint points5[] = {CGPointMake(, ),CGPointMake(, )};
CGContextStrokeLineSegments(ctx, points5, ); CGFloat patterns2[] = {,,,,,,,,,,,,,,,,,};
CGContextSetLineDash(ctx, , patterns2, );
const CGPoint points6[] = {CGPointMake( , ),CGPointMake(, )};
CGContextStrokeLineSegments(ctx, points6, ); //rectangle
CGContextSetStrokeColorWithColor(ctx, [UIColor blueColor].CGColor);
CGContextSetFillColorWithColor(ctx, [UIColor blueColor].CGColor);
CGContextSetLineWidth(ctx, );
CGContextSetLineDash(ctx, , , );//cancel dashed style
CGContextStrokeRect(ctx, CGRectMake(, , , )); CGContextSetStrokeColorWithColor(ctx, [UIColor purpleColor].CGColor);
CGContextSetLineJoin(ctx, kCGLineJoinRound);//round corner
CGContextStrokeRect(ctx, CGRectMake(, , , )); CGContextSetRGBStrokeColor(ctx, 1.0, , 1.0, );
CGContextSetLineJoin(ctx, kCGLineJoinBevel);//cliped corner
CGContextStrokeRect(ctx, CGRectMake(, ,, )); CGContextSetFillColorWithColor(ctx, [UIColor redColor].CGColor);//filled rectangle
CGContextFillRect(ctx, CGRectMake(,, , )); //oval
CGContextSetRGBStrokeColor(ctx, , , , );
CGContextStrokeEllipseInRect(ctx, CGRectMake(, , , ));//Stroke oval CGContextSetRGBFillColor(ctx, , , , );
CGContextFillEllipseInRect(ctx, CGRectMake(, , , ));//Filled oval

draw custom shape

CGContextRef ctx = UIGraphicsGetCurrentContext();//get context
for (int i = ; i < ; i++) {
CGContextBeginPath(ctx);
CGContextAddArc(ctx, i * , i * , (i + ) * , , 1.5 * M_PI, );//actually 0 means clockwise
CGContextClosePath(ctx);
CGContextSetRGBFillColor(ctx, , , , ( - i) * 0.1);
CGContextFillPath(ctx);
}

draw round rect

    CGContextRef ctx = UIGraphicsGetCurrentContext();//get context

    CGContextBeginPath(ctx);

    CGFloat x,y,radius,width,height;
x = ; y = ; radius = ; width = ; height = ;
CGContextMoveToPoint(ctx, x + radius, y);//move to left top corner
CGContextAddLineToPoint(ctx, x + width - radius, y);//add top line to right top corner
CGContextAddArcToPoint(ctx, x + width, y, x + width, y + radius, radius);//add right top corner
CGContextAddLineToPoint(ctx, x + width, y + height - radius);//add right line to right bottom corner
CGContextAddArcToPoint(ctx, x + width, y + height, x + width - radius, y + height, radius);//add right bottom corner
CGContextAddLineToPoint(ctx, x + radius, y + height);//add bottom line to left bottom corner
CGContextAddArcToPoint(ctx, x, y + height, x, y + height - radius, radius);//add left bottom corner
CGContextAddLineToPoint(ctx, x, y + radius);//add left line to left top corner
CGContextAddArcToPoint(ctx, x, y, x + radius, y, radius);//add left top corner CGContextClosePath(ctx);
CGContextSetRGBFillColor(ctx, , , , 0.6);
CGContextFillPath(ctx);

draw nCorner star

CGContextRef ctx = UIGraphicsGetCurrentContext();//get context

    CGContextBeginPath(ctx);

    CGFloat x,y,size;
NSInteger nCorner = ;//n corner
x = ; y = ; size = ;
CGFloat dig = * M_PI / nCorner;CGContextMoveToPoint(ctx, x, y + size);
for (int i = ; i <= nCorner; i++) {
CGFloat _x = sin(i * dig);
CGFloat _y = cos(i * dig);
CGContextAddLineToPoint(ctx, _x * size + x, _y * size + y);
} CGContextClosePath(ctx);
CGContextSetRGBFillColor(ctx, , , , 0.6);
CGContextFillPath(ctx);

draw flower

CGContextRef ctx = UIGraphicsGetCurrentContext();

    CGContextBeginPath(ctx);

    CGFloat x,y,size,length;
NSInteger nCorner = ;//n corner
x = ; y = ; size = ; length = ;//length should be bigger
CGContextMoveToPoint(ctx, x, y + size);
CGFloat dig = * M_PI / nCorner;
for (int i = ; i < nCorner + ; i++) {
//count control point
CGFloat ctrlX = sin((i - 0.5) * dig) * length + x;
CGFloat ctrlY = cos((i - 0.5) * dig) * length + y; //count end point
CGFloat _x = sin(i * dig) * size + x;
CGFloat _y = cos(i * dig) * size + y;
//draw line
CGContextAddQuadCurveToPoint(ctx, ctrlX, ctrlY, _x, _y);
}
CGContextClosePath(ctx);
CGContextSetRGBFillColor(ctx, , , , 0.6);
CGContextFillPath(ctx);

use coordinate

CGContextRef ctx = UIGraphicsGetCurrentContext();

    CGContextTranslateCTM(ctx, , );//move the coordinate
for (int i = ; i < ; i++) {
CGContextSetRGBFillColor(ctx, , , , 0.3 - i * 0.01);
CGContextFillRect(ctx, CGRectMake(, , , ));
CGContextTranslateCTM(ctx, , );
CGContextScaleCTM(ctx, 0.93, 0.93);
CGContextRotateCTM(ctx, - M_PI / ); }
上一篇:perl q qq qr qw qx 区别与使用方法


下一篇:android studio使用的各种问题