人猿星球

UIView动画过程实现交互

2020-04-27  本文已影响0人  爱恨的潮汐

UIView隐式动画默认关闭用户交互,苹果提供一个options选项 UIViewAnimationOptionAllowUserInteraction 允许在动画期间用户交互

[UIView animateWithDuration:5 delay:0 options:UIViewAnimationOptionCurveLinear | UIViewAnimationOptionAllowUserInteraction animations:^{

        CGRect rect = yourView.frame;
        rect.origin = changeOrigin;
        yourView.frame = rect;
   
 } completion:nil];

然后重写View的- (UIView *)hitTest:(CGPoint)point withEvent:(UIEvent *)event方法即可实现用户触摸交互

- (UIView *)hitTest:(CGPoint)point withEvent:(UIEvent *)event{

    UIView *view = [super hitTest:point withEvent:event];
    
    if (!view && !self.isHidden && self.alpha > 0.01) {
        //presentationLayer 是动画渲染过程的图层
        CALayer *presentationLayer = self.layer.presentationLayer;
        CGPoint touchPoint = [self convertPoint:point toView:self.superview];
        if (CGRectContainsPoint(presentationLayer.frame, touchPoint)) {
            view = self;
        }
    }
    
    return view;
}
附代码类:

//【好】UIView执行动画过程中,控件也能点击

.h

#import <UIKit/UIKit.h>

NS_ASSUME_NONNULL_BEGIN

@interface ActionAnmionView : UIView

@end

NS_ASSUME_NONNULL_END

.m

#import "ActionAnmionView.h"

@implementation ActionAnmionView

- (UIView *)hitTest:(CGPoint)point withEvent:(UIEvent *)event{

    UIView *view = [super hitTest:point withEvent:event];
    
    if (!view && !self.isHidden && self.alpha > 0.01) {
        //presentationLayer 是动画渲染过程的图层
        CALayer *presentationLayer = self.layer.presentationLayer;
        CGPoint touchPoint = [self convertPoint:point toView:self.superview];
        if (CGRectContainsPoint(presentationLayer.frame, touchPoint)) {
            view = self;
        }
    }
    
    return view;
}

@end

上一篇 下一篇

猜你喜欢

热点阅读