- (void)drawRect:(CGRect)rect {
/*
UIRectFill(rect);填充矩形函数
UIRectFrame(frame);描边函数
UiBezierPath 绘制常见路径类
*/
//为当前图形上下文设置填充的颜色
[[UIColor whiteColor]setFill];
UIRectFill(rect);
//描边
[[UIColor grayColor]setStroke];
CGRect frame=CGRectMake(0, 40, 320, 400);
UIRectFrame(frame);
//绘制图像和文本,和生成UIimageView一样的效果
UIImage *myimage = [UIImage imageNamed:@"img1.png"];
[myimage drawAtPoint:self.frame.origin];
NSString *s = @"我的鱼";
UIFont *font = [UIFont systemFontOfSize:34];
NSDictionary *attr = @{NSFontAttributeName:font};
[s drawAtPoint:CGPointMake(100, 20) withAttributes:attr];
}
旋转动画
-(void)popPresentView:(UIButton*)sender{
CABasicAnimation *animation = [CABasicAnimation animationWithKeyPath:@"transform.rotation.z"];
//默认是顺时针效果,若将fromValue和toValue的值互换,则为逆时针效果
animation.fromValue = [NSNumber numberWithFloat:0.f];
animation.toValue = [NSNumber numberWithFloat: M_PI /4];
animation.duration = 0.5;
animation.autoreverses = NO;
animation.fillMode =kCAFillModeForwards;
animation.repeatCount = 1;
[sender.layer addAnimation:animation forKey:nil];
// [UIView animateWithDuration:1.5 animations:^{
// self.hidden = YES;
// }];
[self performSelector:@selector(hideView) withObject:self afterDelay:0.5f];
}
上下滑动虚动画
//动画效果实现
-(void)animationFromView:(UIView*)view fromTop:(BOOL)top{
CABasicAnimation *baseAnimation = [CABasicAnimation animation];
baseAnimation.duration = 0.5f;
baseAnimation.keyPath = @"transform.translation";
//baseAnimation.delegate = self;
if (top) {
baseAnimation.fromValue = [NSValue valueWithCGPoint:CGPointMake(self.frame.origin.x, -100)];
}else{
baseAnimation.fromValue = [NSValue valueWithCGPoint:CGPointMake(self.frame.origin.x, LWLWindowsH+100)];
}
baseAnimation.toValue = [NSValue valueWithCGPoint:CGPointMake(self.frame.origin.x, view.frame.origin.y)];
baseAnimation.removedOnCompletion=YES;
baseAnimation.fillMode = kCAFillModeForwards;
[view.layer addAnimation:baseAnimation forKey:nil];
}
//动画汇总
- (void)viewDidLoad {
[super viewDidLoad];
self.view.backgroundColor = [UIColor whiteColor];
_demoView = [UIView new];
[self.view addSubview:_demoView];
_demoView.backgroundColor = [UIColor redColor];
_demoView.frame = CGRectMake(0, HEIGHT/2-50, 50, 50);
// Do any additional setup after loading the view.
[self test3];
}
-(void)test1{
[UIView animateWithDuration:1.0f animations:^{
_demoView.frame = CGRectMake(WIDTH, HEIGHT/2-50, 50, 50);
} completion:^(BOOL finished) {
_demoView.frame = CGRectMake(WIDTH/2-50, HEIGHT/2-50, 50, 50);
}];
}
-(void)test2{
[UIView beginAnimations:nil context:nil];
[UIView setAnimationDuration:1.0f];
_demoView.frame = CGRectMake(WIDTH/2-50, HEIGHT/2-50, 50, 50);
[UIView commitAnimations];
}
//位移,透明度,缩放,旋转,背景色
-(void)test3{
/*
基础动画(CABaseAnimation)
重要属性
fromValue:keyPath对应的初始值
toValue:keyPath对应的结束值
基础动画主要提供了对于CALayer对象中的可变属性进行简单动画的操作。比如:位移、透明度、缩放、旋转、背景色等等。
*/
CABasicAnimation *animation = [CABasicAnimation animationWithKeyPath:@"position"];
animation.fromValue = [NSValue valueWithCGPoint:CGPointMake(0, HEIGHT/2-75)];
animation.toValue = [NSValue valueWithCGPoint:CGPointMake(WIDTH, HEIGHT/2-75)];
animation.duration = 1.0f;
animation.fillMode = kCAFillModeForwards;
/*
kCAFillModeForwards
kCAFillModeBoth;
kCAFillModeRemoved;
kCAFillModeBackwards
*/
//动画结束后是否保持原样
animation.removedOnCompletion = NO;
[_demoView.layer addAnimation:animation forKey:@"positionANimation"];
}
-(void)test4{
//关键帧动画
/*
关键帧动画(CAKeyframeAnimation)
CAKeyframeAnimation和CABaseAnimation都属于CAPropertyAnimatin的子类。CABaseAnimation只能从一个数值(fromValue)变换成另一个数值(toValue),而CAKeyframeAnimation则会使用一个NSArray保存一组关键帧。
重要属性
values:就是上述的NSArray对象。里面的元素称为”关键帧”(keyframe)。动画对象会在指定的时间(duration)内,依次显示values数组中的每一个关键帧
path:可以设置一个CGPathRef\CGMutablePathRef,让层跟着路径移动。path只对CALayer的anchorPoint和position起作用。如果你设置了path,那么values将被忽略。
keyTimes:可以为对应的关键帧指定对应的时间点,其取值范围为0到1.0,keyTimes中的每一个时间值都对应values中的每一帧.当keyTimes没有设置的时候,各个关键帧的时间是平分的。
*/
CAKeyframeAnimation *anima = [CAKeyframeAnimation animationWithKeyPath:@"position"];
UIBezierPath *path = [UIBezierPath bezierPathWithOvalInRect:CGRectMake(WIDTH/2-100, HEIGHT/2-100, 200, 200)];
anima.path = path.CGPath;
anima.duration = 2.0f;
[_demoView.layer addAnimation:anima forKey:@"pathAnimation"];
}
-(void)test5{
/*
组动画(CAAnimationGroup)
CAAnimation的子类,可以保存一组动画对象,将CAAnimationGroup对象加入层后,组中所有动画对象可以同时并发运行。
重要属性
animations: 用来保存一组动画对象的NSArray
*/
CAKeyframeAnimation *anima1 = [CAKeyframeAnimation animationWithKeyPath:@"position"];
NSValue *value0 = [NSValue valueWithCGPoint:CGPointMake(0, SCREEN_HEIGHT/2-50)];
NSValue *value1 = [NSValue valueWithCGPoint:CGPointMake(SCREEN_WIDTH/3, SCREEN_HEIGHT/2-50)];
NSValue *value2 = [NSValue valueWithCGPoint:CGPointMake(SCREEN_WIDTH/3, SCREEN_HEIGHT/2+50)];
NSValue *value3 = [NSValue valueWithCGPoint:CGPointMake(SCREEN_WIDTH*2/3, SCREEN_HEIGHT/2+50)];
NSValue *value4 = [NSValue valueWithCGPoint:CGPointMake(SCREEN_WIDTH*2/3, SCREEN_HEIGHT/2-50)];
NSValue *value5 = [NSValue valueWithCGPoint:CGPointMake(SCREEN_WIDTH, SCREEN_HEIGHT/2-50)];
anima1.values = [NSArray arrayWithObjects:value0,value1,value2,value3,value4,value5, nil];
//缩放动画
CABasicAnimation *anima2 = [CABasicAnimation animationWithKeyPath:@"transform.scale"];
anima2.fromValue = [NSNumber numberWithFloat:0.8f];
anima2.toValue = [NSNumber numberWithFloat:2.0f];
//旋转动画
CABasicAnimation *anima3 = [CABasicAnimation animationWithKeyPath:@"transform.rotation"];
anima3.toValue = [NSNumber numberWithFloat:M_PI*4];
//组动画
CAAnimationGroup *groupAnimation = [CAAnimationGroup animation];
groupAnimation.animations = [NSArray arrayWithObjects:anima1,anima2,anima3, nil];
groupAnimation.duration = 4.0f;
[_demoView.layer addAnimation:groupAnimation forKey:@"groupAnimat"];
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
-(void)test6{
/*过渡动画(CATransition)
CAAnimation的子类,用于做过渡动画或者转场动画,能够为层提供移出屏幕和移入屏幕的动画效果。
重要属性
type:动画过渡类型
Apple 官方的SDK其实只提供了四种过渡效果。
kCATransitionFade 渐变效果
kCATransitionMoveIn 进入覆盖效果
kCATransitionPush 推出效果
kCATransitionReveal 揭露离开效果
私有API提供了其他很多非常炫的过渡动画,比如@"cube"、@"suckEffect"、@"oglFlip"、 @"rippleEffect"、@"pageCurl"、@"pageUnCurl"、@"cameraIrisHollowOpen"、@"cameraIrisHollowClose"等。
注意点
私有api,不建议开发者们使用。因为苹果公司不提供维护,并且有可能造成你的app审核不通过。
subtype:动画过渡方向
kCATransitionFromRight 从右侧进入
kCATransitionFromLeft 从左侧进入
kCATransitionFromTop 从顶部进入
kCATransitionFromBottom 从底部进入
startProgress:动画起点(在整体动画的百分比)
endProgress:动画终点(在整体动画的百分比)
*/
}