2014.12.15 ——— ios开发之hitTest pointInside

2014.12.15———ios开发之hitTestpointInside

参考:http://blog.csdn.net/bravegogo/article/details/19936689

两个方法的意思可以参考上面这边文章

我用到的场景主要是:

UITableView每一个cell右划出现删除,点击其他地方取消删除按钮

思路:

捕获右划,建立一个UITableView的子view,大小与其一样,并且这个子view重写hitTest方法,来判断是否点击了删除按钮

代码:

#import <UIKit/UIKit.h>

@interface MyView : UIView
{
}

@property(nonatomic, assign) UIView *ignoreView;

- (id)initWithParentView:(UIView *)parent ignoreView:(UIView *)ignoreView;
#import "MyView.h"
@implementaion MyView

- (id)initWithParentView:(UIView *)parent ignoreView:(UIView *)ignoreView{
   self = [[MyView alloc] init];
   if(self){
        self.frame = parent.frame;
        self.backgroundColor = [UIColor clearColor];
        [parent addSubView:self];
        _ignoreView = ignoreView;
        self.userInteractionEnabled = YES;
   }
   return self;
}

- (UIView *)hitTest:(CGPoint)point withEvent:(UIEvent *)event{
   UIView *result = [super hitTest:point withEvent:event];
   CGPoint buttonPoint = [_ignoreView convertPoint:point fromView:self];
   if([_ignoreView pointInside:buttonPoint withEvent:event]){
       return _ignoreView;
   }
   return result;
}

这样之后当点击到删除按钮了,虽然删除按钮其实没有捕获到但是通过hitTest指向了删除按钮,就可以再删除按钮的代理方法来处理逻辑了

相关推荐