美文网首页
iOS 区域响应交互

iOS 区域响应交互

作者: CaptainRoy | 来源:发表于2019-03-18 19:08 被阅读0次
  • 在一个正方形的按钮中只有圆形区域可以响应
//  获取响应的视图
- (UIView *)hitTest:(CGPoint)point withEvent:(UIEvent *)event
// 获取响应位置
- (BOOL)pointInside:(CGPoint)point withEvent:(UIEvent *)event
  • CustomButton 类
#import <UIKit/UIKit.h>

@interface CustomButton : UIButton

@end
#import "CustomButton.h"

@implementation CustomButton


- (UIView *)hitTest:(CGPoint)point withEvent:(UIEvent *)event
{
    if (!self.userInteractionEnabled || [self isHidden] || self.alpha <= 0.01) { // 如果是禁止交互,隐藏,透明则不用响应 返回 nil
        return nil;
    }
    
    if ([self pointInside:point withEvent:event]) {
        //遍历当前对象的子视图
        __block UIView *hit = nil;
        [self.subviews enumerateObjectsWithOptions:NSEnumerationReverse usingBlock:^(__kindof UIView * _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
            // 坐标转换
            CGPoint vonvertPoint = [self convertPoint:point toView:obj];
            //调用子视图的hittest方法
            hit = [obj hitTest:vonvertPoint withEvent:event];
            // 如果找到了接受事件的对象,则停止遍历
            if (hit) {
                *stop = YES;
            }
        }];
        
        if (hit) {
            return hit;
        }
        else{
            return self;
        }
    }
    else{
        return nil;
    }
}

- (BOOL)pointInside:(CGPoint)point withEvent:(UIEvent *)event
{
    CGFloat x1 = point.x;
    CGFloat y1 = point.y;
    
    CGFloat x2 = self.frame.size.width / 2;
    CGFloat y2 = self.frame.size.height / 2;
    
    double dis = sqrt((x1 - x2) * (x1 - x2) + (y1 - y2) * (y1 - y2));
    // 67.923
    if (dis <= self.frame.size.width / 2) {
        return YES;
    }
    else{
        return NO;
    }
}
CustomButton *customButton = [[CustomButton alloc] initWithFrame:CGRectMake(100, 100, 120, 120)];
    customButton.backgroundColor = [UIColor blueColor];
    [customButton addTarget:self action:@selector(doAction:) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:customButton];

相关文章

  • iOS 区域响应交互

    在一个正方形的按钮中只有圆形区域可以响应 CustomButton 类

  • iOS基础06—--事件响应链

    iOS基础06—--事件响应链 移动应用的最大特性就是响应用户交互操作,那么iOS系统是如何去响应一个简单的点击事...

  • 一个分类解决按钮的点击区域与重复点击问题!

    iOS中,要改变响应区域,首相想到的是响应者链条,然后就想到 - (nullable UIView *)hitTe...

  • iOS 事件传递和处理

    前言 iPhone拥有很好的用户交互体验,这源于iOS系统对交互事件的高效处理和高优响应;App开发者处理用户交互...

  • iOS 事件传递和处理

    前言 iPhone拥有很好的用户交互体验,这源于iOS系统对交互事件的高效处理和高优响应;App开发者处理用户交互...

  • iOS 响应者链

    概述 iOS 响应者链(Responder Chain) 是支持App界面交互的重要基础, 点击, 滑动, 旋转,...

  • UIWindow知识归纳

    UIView的功能 负责渲染区域的内容,并且响应该区域内发生的触摸事件 UIWindow 在iOS App中,UI...

  • 网络安全交互的常用方式

    后端经常需要与web,andorid,ios交互,有的时候前后端交互的请求和响应数据的进行明文传输,接口也没有做严...

  • iOS事件的传递链和响应链

    彻底理解事件的传递链和响应链需要先弄明白iOS对象为什么可以响应用户交互,理解UIResponder类; 1.1响...

  • 增加点击区域

    项目中很多时候需要扩大点击(交互)区域,或子试图超出了父视图后,无法点击或交互等,我们可以通过响应者链-(UIVi...

网友评论

      本文标题:iOS 区域响应交互

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