zoukankan      html  css  js  c++  java
  • ios 给移动的控件添加点击事件

    前言:

    给一个UIView做移动动画,虽然看起来frame在持续改变,但是它的frame已经是最终值了. 也就是说表面看到的动画都是假象,它的真实位置已经是固定的了.所以只有点击在他的真实frame范围内,点击事件才会响应. 

    其实UIview的layer 有一个属性叫presentationLayer. 一个方法:- (nullable CALayer *)hitTest:(CGPoint)p;只要使用if ([view.layer.presentationLayer hitTest:touchPoint]) { } 判断一下即可.知晓点是不是在移动的view上.

    代码:

    - (id)initWithFrame:(CGRect)frame {
        self = [super initWithFrame:frame];
        if (self) {
                //给移动view的父视图添加单击手势
                UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(addTap:)];
                [self addGestureRecognizer:tap];
            }
        }   
        return self;
    }
    -(void)addTap:(UITapGestureRecognizer *)tapGesture{
        //如果手势点在移动view的父视图上,返回这个点.
        CGPoint touchPoint = [tapGesture locationInView:self];
        
        //遍历父视图的所有子视图
        for (PQBarrageItemView *item in self.subviews) {
            
            //判断点是不是在移动的view上
            if ([item.layer.presentationLayer hitTest:touchPoint]) {
                NSLog(@"此处写点在移动view之后的代码");
            }
        }
    }

     

  • 相关阅读:
    Balanced Binary Tree
    Swap Nodes in Pairs
    Reverse Nodes in k-Group
    Reverse Linked List II
    Remove Nth Node From End of List
    Remove Duplicates from Sorted List II
    Remove Duplicates from Sorted List
    Partition List
    Merge Two Sorted Lists
    【Yii2.0】1.2 Apache检查配置文件语法
  • 原文地址:https://www.cnblogs.com/jiayongqiang/p/5519271.html
Copyright © 2011-2022 走看看