分享

iOS  手势和动画

 牛人的尾巴 2015-08-19

iOS  手势和动画

(2015-01-09 17:13:12)
标签:

ios

手势

动画

分类: iOS
平时项目中用动画比较多使用UIView类的UIViewAnimation扩展

UIView(UIViewAnimation)

UIView(UIViewAnimationWithBlocks)

一般用得比较多得是:

+ (void)animateWithDuration:(NSTimeInterval)duration delay:(NSTimeInterval)delay options:(UIViewAnimationOptions)options animations:(void (^)(void))animations completion:(void (^)(BOOL finished))completion NS_AVAILABLE_IOS(4_0);


 

+ (void)animateWithDuration:(NSTimeInterval)duration animations:(void (^)(void))animations completion:(void (^)(BOOL finished))completion NS_AVAILABLE_IOS(4_0); // delay = 0.0, options = 0

今天把手势和动画结合使用了下,手势有

UITapGestureRecognizer        点击

UILongPressGestureRecognizer  长按

UIPanGestureRecognizer        移动

点击的时候实现放大,长按的时候实现旋转,然后移动的时候实现位置互换点击空白区域的时候动画取消,还原

具体实现如下:

点击:

[UIView animateWithDuration:1 animations:^{

        //

        for (UIButton *button in self.view.subviews)

        {

            

            button.transform=CGAffineTransformIdentity;

            button.alpha=1.0;

        }

    } completion:^(BOOL finished) {

        //

            }];

    [UIView animateWithDuration:1 animations:^{

        sender.transform=CGAffineTransformMakeScale(1.5, 1.5);

        sender.alpha=0.5;

    } completion:^(BOOL finished) {

        //

 

    }];


长按

if (sender.state==UIGestureRecognizerStateBegan)

    {

        for (UIButton *button in self.view.subviews)

        {

            NSUInteger index=  [self.view.subviews indexOfObject:button];

           if ((int)index%2==0)

            {

                button.transform=CGAffineTransformMakeRotation(0.3);

            }

            else

            {

                button.transform=CGAffineTransformMakeRotation(-0.3);

            }

        }

        [UIView animateWithDuration:0.2 delay:0.0 options:UIViewAnimationOptionAutoreverse|UIViewAnimationOptionRepeat animations:^{

            for (UIButton *ibutton in self.view.subviews) {

                NSUInteger iindex=  [self.view.subviews indexOfObject:ibutton];

                if (iindex%2==1)

                {

                    ibutton.transform=CGAffineTransformMakeRotation(0.3);

                }

                else

                {

                    ibutton.transform=CGAffineTransformMakeRotation(-0.3);

                }

            }

        } completion:^(BOOL finished) {

            //

            [UIView animateWithDuration:1.0 delay:0.0 options:UIViewAnimationOptionCurveEaseInOut animations:^{

                //

                for (UIButton *button in self.view.subviews)

                {

                    button.transform=CGAffineTransformMakeScale(1, 1);

                    button.alpha=1.0;

                }

            } completion:^(BOOL finished) {

                //

            }];

        }];

    }

    else if (sender.state==UIGestureRecognizerStateChanged)

    {

        NSLog(@"UIGestureRecognizerStateChanged");

    }

    else

    {

        NSLog(@"UIGestureRecognizerStateEnded");

    }

移动

 if(sender.state==UIGestureRecognizerStateBegan)

    {

        UIButton *sbutton=(UIButton *)sender.view;

        NSLog(@"%@",NSStringFromCGRect(sbutton.frame));

         rstart=sbutton.frame;

        

    }

    else if (sender.state==UIGestureRecognizerStateChanged)

    {

        CGPoint center=[sender locationInView:self.view];

        [sender.view setCenter:center];

    }

    else if (sender.state==UIGestureRecognizerStateEnded)

    {

        CGPoint point=[sender translationInView:self.view];

        //CGRect end=CGRectMake(rstart.origin.x+point.x, rstart.origin.y+point.y, rstart.size.width, rstart.size.height);

        

        for (UIButton *button in self.view.subviews)

        {

            if (button!=sender.view)

            {

                if (CGRectContainsPoint(button.frame,sender.view.center))

                {

                    [UIView animateWithDuration:2.0 animations:^{

                        //

                        NSLog(@"%@",NSStringFromCGRect(rstart));

                        [sender.view setFrame:button.frame];

                        [button setFrame:rstart];

                        

                        

                    } completion:^(BOOL finished) {

                        //

                    }];

                }

            }

        }

 

    }


    本站是提供个人知识管理的网络存储空间,所有内容均由用户发布,不代表本站观点。请注意甄别内容中的联系方式、诱导购买等信息,谨防诈骗。如发现有害或侵权内容,请点击一键举报。
    转藏 分享 献花(0

    0条评论

    发表

    请遵守用户 评论公约

    类似文章 更多