美文网首页人猿星球
UIView动画过程实现交互

UIView动画过程实现交互

作者: 爱恨的潮汐 | 来源:发表于2020-04-27 10:05 被阅读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
    
    

    相关文章

      网友评论

        本文标题:UIView动画过程实现交互

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