IOS之CAAnimation基本动画


#import "MainViewController.h"
@interface MainViewController ()
@property (weak, nonatomic) UIView *myView;
@end
@implementation MainViewController
/*
 要是先简单动画,通过touchBegan方法来触发
 1. 平移动画
 */
- (void)viewDidLoad
{
    [super viewDidLoad];
    UIView *myView = [[UIView alloc]initWithFrame:CGRectMake(50, 50, 100, 100)];
    [myView setBackgroundColor:[UIColor redColor]];
    [self.view addSubview:myView];
    self.myView = myView;
}
#pragma mark - 动画代理方法
#pragma mark 动画开始(极少用)
- (void)animationDidStart:(CAAnimation *)anim
{
    NSLog(@"开始动画");
}
#pragma mark 动画结束(通常在动画结束后,做动画的后续处理)
- (void)animationDidStop:(CAAnimation *)anim finished:(BOOL)flag
{
    NSString *type = [anim valueForKey:@"animationType"];
    if ([type isEqualToString:@"translationTo"]) {
        // 1. 通过键值取出需要移动到的目标点
        CGPoint point = [[anim valueForKey:@"targetPoint"]CGPointValue];
        NSLog(@"目标点: %@", NSStringFromCGPoint(point));
        // 2. 设置myView的坐标点
        [self.myView setCenter:point];
    }
    NSLog(@"结束动画,myView: %@", NSStringFromCGRect(self.myView.frame));
}
#pragma mark - touch事件
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
    UITouch *touch = touches.anyObject;
    CGPoint location = [touch locationInView:self.view];

    if ([touch view] == self.myView) {
        NSLog(@"点击myView");
    }

//    [self.myView setCenter:location];
    // 将myView平移到手指触摸的目标点
//    [self translationTo:location];

    [UIView animateWithDuration:1.0f animations:^{
        [self.myView setCenter:location];
    } completion:^(BOOL finished) {
        NSLog(@"%@", NSStringFromCGRect(self.myView.frame));
    }];
}

#pragma mark - CABasic动画
#pragma mark 平移动画到指定点
- (void)translationTo:(CGPoint)point
{
    // 1. 实例化动画
    // 如果没有指定图层的锚点(定位点)postion对应UIView的中心点
    CABasicAnimation *anim = [CABasicAnimation animationWithKeyPath:@"position"];

    // 2. 设置动画属性
    // 1) fromValue(myView的当前坐标) & toValue
    [anim setToValue:[NSValue valueWithCGPoint:point]];

    // 2) 动画的时长
    [anim setDuration:1.0f];

    // 3) 设置代理
    [anim setDelegate:self];

    // 4) 让动画停留在目标位置
    /*
     提示:通过设置动画在完成后不删除,以及向前填充,可以做到平移动画结束后,
     UIView看起来停留在目标位置,但是其本身的frame并不会发生变化
     */
    [anim setRemovedOnCompletion:NO];
    // forwards是逐渐逼近目标点
    [anim setFillMode:kCAFillModeForwards];

    // 5) 要修正坐标点的实际位置可以利用setValue方法
    [anim setValue:[NSValue valueWithCGPoint:point] forKey:@"targetPoint"];
    [anim setValue:@"translationTo" forKey:@"animationType"];

    // 3. 将动画添加到图层
    // 将动画添加到图层之后,系统会按照定义好的属性开始动画,通常程序员不在与动画进行交互
    [self.myView.layer addAnimation:anim forKey:nil];
}

@end
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值