简体   繁体   中英

iOS Animation Block Gesture Recognizer

I have a UICollectionView containing a number of cells containing views which can drag/dropped out of the collection view into a different view that is outside the collection view. This process works fine. However, when the dragged view is dropped into its new location, I want to animate the drop by scaling the drag view to its full size then back to zero, before removing it from the superview. This works in other areas of the app when I'm dragging other objects around, but this one is the only one involving the collection view.

[UIView animateWithDuration:0.375
                 animations:^{ dragView.transform = CGAffineTransformMakeScale (1.0f, 1.0f); dragView.transform = CGAffineTransformMakeScale(0.0f, 0.0f); }
                 completion:^(BOOL finished) {  [dragView removeFromSuperview]; }  ];   

If I don't use the completion block, the animation fails, presumably because the view is removed before the animation finishes. But if I DO use the completion block, when the animation completes, subsequent pan gestures (used for scrolling the collection view) are passed to the pan gesture recognizer used in my view controller for other things, instead of for scrolling the collection view. As a result, the collection view appears "locked up" after the animation. If I remove the completion block, the problem with gesture recognition doesn't occur afterward, but the animation doesn't work, either.

I've tried setting userInteractionEnabled=YES on the collection view after the animation, but it doesn't help.

Any suggestions? TIA

omg, what do you expect of 2 simultaneous animations of the same type? Maybe it is a solution?

first animation call:

[UIView animateWithDuration:0.375
                 animations:^{ dragView.transform = CGAffineTransformMakeScale (1.0f, 1.0f); }
                 completion:^(BOOL finished) {  /*call the second animation*/ }  ];

second animation call:

//second animation
[UIView animateWithDuration:0.375
                 animations:^{ dragView.transform = CGAffineTransformMakeScale(0.0f, 0.0f); }
                 completion:^(BOOL finished) {  [dragView removeFromSuperview]; }  ]; 

In Swift 5.0, iOS 13+, add .allowUserInteraction to animate options, then animation won't block gesture recognizer.

UIView.animate(
withDuration: 0.375,
delay: 0,
options: [.curveEaseOut, .allowUserInteraction],
animations: {
    dragView.transform = CGAffineTransform(scaleX: 1.0, y: 1.0)
    dragView.transform = CGAffineTransform(scaleX: 0.0, y: 0.0)
},
completion: {_ in dragView.removeFromSuperview()})

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM