美文网首页
UI- --基础动画

UI- --基础动画

作者: 單戈 | 来源:发表于2015-11-11 17:08 被阅读0次

    //初始化UIview

    _animationView = [[UIView alloc]init];

    _animationView.bounds = CGRectMake(0, 0, 200, 200);

    _animationView.center = CGPointMake(CGRectGetMidX(self.view.bounds), CGRectGetMidY(self.view.bounds));

    _animationView.backgroundColor = [UIColor redColor];

    [self.view addSubview:_animationView];

    UITapGestureRecognizer *tapGesture = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(tapAnimationView:)];

    //为_animationView添加单机手势

    [_animationView addGestureRecognizer:tapGesture];

    //拖动手势

    UIPanGestureRecognizer *pan = [[UIPanGestureRecognizer alloc]initWithTarget:self action:@selector(panAnima:)];

    [_animationView addGestureRecognizer:pan];

    //缩放手势

    UIPinchGestureRecognizer *pinchGe = [[UIPinchGestureRecognizer alloc]initWithTarget:self action:@selector(pinchGeAnima:)];

    [_animationView addGestureRecognizer:pinchGe];

    //缩放

    - (void)pinchGeAnima:(UIPinchGestureRecognizer *)gesture{

    if (gesture.state ==UIGestureRecognizerStateChanged) {

    gesture.view.transform = CGAffineTransformScale(gesture.view.transform, gesture.scale,gesture.scale);

    //保持缩放系数为1

    gesture.scale = 1;

    }else if (gesture.state == UIGestureRecognizerStateEnded){

    //取消一切形变

    //        _animationView.transform = CGAffineTransformIdentity;取消上面的缩放,变回原图

    }

    }

    //拖动

    - (void)panAnima:(UIPanGestureRecognizer *)gesture{

    if (gesture.state == UIGestureRecognizerStateChanged) {

    //拖动方式写这里,获取手势在指定视图的移动坐标

    CGPoint transition = [gesture translationInView:self.view];

    NSLog(@"%@",NSStringFromCGPoint(transition));

    gesture.view.center = CGPointMake(gesture.view.center.x+transition.x, gesture.view.center.y+transition.y);

    //重新设置UIGestureRecognizer的增量

    [gesture setTranslation:CGPointZero inView:self.view];

    }else if(gesture.state == UIGestureRecognizerStateEnded){

    }

    }

    - (void)tapAnimationView:(UITapGestureRecognizer *)gesture{

    /**

    //--------------------------------1.第一种动画的方式。普通方式

    //开始动画

    [UIView beginAnimations:nil context:nil];

    //设置代理,才能回调动画执行完毕后的方法

    [UIView setAnimationDelegate:self];

    //设置动画持续时间

    [UIView setAnimationDuration:0.5];

    //设置动画的线性规律

    [UIView setAnimationCurve:UIViewAnimationCurveEaseInOut];

    //设置动画的执行完成后的回调方法

    [UIView setAnimationDidStopSelector:@selector(stopAnimating)];

    //这里写动画

    //改变动画的center

    _animationView.center = CGPointMake(CGRectGetMidX(self.view.bounds), CGRectGetMidY(self.view.bounds)+150);    //提交动画

    [UIView commitAnimations];

    **/

    /**

    //-------------------------------2.第二种执行动画的方式,block

    [UIView animateWithDuration:1 animations:^{

    _animationView.center = CGPointMake(CGRectGetMidX(self.view.bounds), CGRectGetMidY(self.view.bounds)+150);

    }completion:^(BOOL finished) {//再写个block执行动画回调

    _animationView.backgroundColor = [UIColor yellowColor];

    }];

    **/

    //旋转  翻转动画

    [self animationvieWithAnimation];

    }

    //选装翻转动画方法

    - (void)animationvieWithAnimation{

    [UIView animateKeyframesWithDuration:2 delay:0 options:UIViewKeyframeAnimationOptionAllowUserInteraction animations:^{

    //缩放

    //        _animationView.transform = CGAffineTransformMakeScale(0.5, 0.5);

    //旋转

    //        _animationView.transform = CGAffineTransformMakeRotation(M_PI);

    //缩放

    CGAffineTransform transform = CGAffineTransformMakeScale(0.5, 0.5);

    _animationView.transform = CGAffineTransformRotate(transform, M_PI);

    //CGAffineTransformRotate保存 了前一个的状态,缩放状态

    } completion:^(BOOL finished) {

    //翻转

    [UIView transitionWithView:_animationView duration:2 options:UIViewAnimationOptionCurveEaseInOut |UIViewAnimationOptionTransitionFlipFromLeft animations:^{

    _animationView.backgroundColor = [UIColor blackColor];

    } completion:^(BOOL finished) {

    }];

    }];

    }

    - (void)stopAnimating{

    _animationView.backgroundColor = [UIColor blackColor];

    }

    相关文章

      网友评论

          本文标题:UI- --基础动画

          本文链接:https://www.haomeiwen.com/subject/fauhhttx.html