使用绘图,动画

1.

CGMutablePathRef starPath = CGPathCreateMutable();

CGPathMoveToPoint(starPath,NULL,100.0f, 100.0f);

CGPathAddLineToPoint(starPath, NULL, 100.0f, 280.0f);

CGPathAddLineToPoint(starPath, NULL, 260.0, 170.0);

CGPathAddLineToPoint(starPath, NULL, 60.0, 170.0);

CGPathAddLineToPoint(starPath, NULL, 220.0, 280.0);

CGPathCloseSubpath(starPath);//记得这一点

2>.将图片渲染到上下文中

NSString *path = [[NSBundle mainBundle] pathForResource:@"cat" ofType:@"jpg"];

UIImage *img = [UIImage imageWithContentsOfFile:path];

CGImageRef image = img.CGImage;//将UIImage转化为CGImageRef

CGContextRef context = UIGraphicsGetCurrentContext();//开启上下文

CGContextSaveGState(context);

 CGContextSaveGState :保存当前的绘图状态。 

 CGContextRestoreGState :恢复之前保存的绘图状态。

CGAffineTransform myAffine = CGAffineTransformMakeRotation(M_PI); 

myAffine = CGAffineTransformTranslate(myAffine, -img.size.width, -img.size.height); 

CGContextConcatCTM(context, myAffine);//CGContextTranslateCTM平移坐标系统。 CGContextScaleCTM缩放坐标系统 CGContextRotateCTM旋转坐标系统  

CGRect touchRect = CGRectMake(0, 0, img.size.width, img.size.height);

CGContextDrawImage(context, touchRect, image);  

CGContextRestoreGState(context);

//画弧形 

CGContextAddArc(CGContextRef c, CGFloat x, CGFloat y, CGFloat radius, CGFloat startAngle, CGFloat endAngle, int clockwise)

x,y为圆点坐标,startAngle为开始的弧度,endAngle为 结束的弧度,clockwise 0为顺时针,1为逆时针

3>.画折线

- (void)drawRect:(CGRect)rect {

CGContextRef cgContext = UIGraphicsGetCurrentContext();

    CGContextMoveToPoint(cgContext, 333, 0);

    CGContextAddCurveToPoint(cgContext, 333, 0, 332, 26, 330, 26);

    CGContextAddCurveToPoint(cgContext, 330, 26, 299, 20, 299, 17);

    CGContextAddLineToPoint(cgContext, 296, 17);

    CGContextAddCurveToPoint(cgContext, 296, 17, 296, 19, 291, 19);

    CGContextAddLineToPoint(cgContext, 250, 19);

    CGContextAddCurveToPoint(cgContext, 250, 19, 241, 24, 238, 19);

    CGContextAddCurveToPoint(cgContext, 236, 20, 234, 24, 227, 24);

    CGContextAddCurveToPoint(cgContext, 220, 24, 217, 19, 216, 19);

    CGContextAddCurveToPoint(cgContext, 214, 20, 211, 22, 207, 20);

    CGContextAddCurveToPoint(cgContext, 207, 20, 187, 20, 182, 21);

    CGContextAddLineToPoint(cgContext, 100, 45);

    CGContextAddLineToPoint(cgContext, 97, 46);

    CGContextAddCurveToPoint(cgContext, 97, 46, 86, 71, 64, 72);

    CGContextAddCurveToPoint(cgContext, 42, 74, 26, 56, 23, 48);

    CGContextAddLineToPoint(cgContext, 9, 47);

    CGContextAddCurveToPoint(cgContext, 9, 47, 0, 31, 0, 0);

    CGContextStrokePath(cgContext);

}

2.关键帧动画

1>关于位置

<CAAnimationDelegate>//如果监听动画的执行过程就要设置代理

CAKeyframeAnimation *animation = nil;

animation = [CAKeyframeAnimation animationWithKeyPath:@"position"];

[animation setDuration:10.0f];

[animation setDelegate:self];

[animation setPath:starPath];

CFRelease(starPath);//记得这一点

starPath = nil;

[[imageView layer] addAnimation:animation forKey:@"position"];

//比如监听动画执行完了

- (void)animationDidStop:(CAAnimation *)theAnimation finished:(BOOL)flag {

}

2>关于透明度

CAKeyframeAnimation *opAnim = [CAKeyframeAnimation animationWithKeyPath:@"opacity"];

opAnim.duration = 6.0;

opAnim.values =[NSArray arrayWithObjects: 

[NSNumber numberWithFloat:0.25],  

[NSNumber numberWithFloat:0.75], 

[NSNumber numberWithFloat:1.0],

nil]; 

opAnim.keyTimes = [NSArray arrayWithObjects:

  [NSNumber numberWithFloat:0.0], 

  [NSNumber numberWithFloat:0.5], 

  [NSNumber numberWithFloat:1.0], nil];

[view.layer addAnimation:opAnim forKey:@"animateOpacity"];

 3.基本动画

1>关于透明度

CABasicAnimation *opAnim = [CABasicAnimation animationWithKeyPath:@"opacity"];

opAnim.duration = 2.0;

opAnim.fromValue = [NSNumber numberWithFloat:.20];

opAnim.toValue= [NSNumber numberWithFloat:1.0];

opAnim.cumulative = YES;

opAnim.repeatCount = 2;

[view.layer addAnimation:opAnim forKey:@"animateOpacity"];

2>关于控件的大小位置的 

CGAffineTransform moveTransform = CGAffineTransformMakeTranslation(180, 200);

CABasicAnimation *moveAnim = [CABasicAnimation animationWithKeyPath:@"transform"];

moveAnim.duration = 6.0;

moveAnim.toValue= [NSValue valueWithCATransform3D:

CATransform3DMakeAffineTransform(moveTransform)];

[view.layer addAnimation:moveAnim forKey:@"animateTransform"];

3.首尾动画

1>.

[UIView beginAnimations:nil context:NULL];

CGAffineTransform moveTransform = CGAffineTransformMakeTranslation(180, 200);

view.layer.affineTransform=moveTransform;

[UIView commitAnimations];

2>.

[UIView beginAnimations:@"animationID" context:nil];

[UIView setAnimationDuration:1.5f];

[UIView setAnimationCurve:UIViewAnimationCurveEaseInOut];

[UIView setAnimationRepeatAutoreverses:NO];

[UIView setAnimationTransition:UIViewAnimationTransitionFlipFromLeft forView:self.view cache:YES];

 [UIView setAnimationDidStopSelector:@selector(viewAnimationDone:)];//类似上面的代理监听动画的完成

[UIView commitAnimations];

原文地址:https://www.cnblogs.com/chaoyueME/p/6272372.html