当前位置: 代码网 > it编程>App开发>苹果IOS > iOS超出父控件范围无法点击问题解决

iOS超出父控件范围无法点击问题解决

2024年05月15日 苹果IOS 我要评论
场景橙色view添加在蓝色view上,满足点击超出蓝色view部分可以响应事件实现思路重写底部蓝色view的hittest方法,从最上层依次遍历子控件,判断触摸点是否在子控件上,在的话就返回子控件的h

场景

橙色view添加在蓝色view上,满足点击超出蓝色view部分可以响应事件

实现思路

重写底部蓝色view的hittest方法,从最上层依次遍历子控件,判断触摸点是否在子控件上,在的话就返回子控件的hittest方法,不在就返回self

完整代码

#import "viewcontroller.h"
#import "botview.h"
@interface viewcontroller ()
@property(strong, nonatomic) botview *botview;
@property(strong, nonatomic) uiview *topview;
@end
@implementation viewcontroller
- (void)viewdidload {
    [super viewdidload];
    self.view.backgroundcolor = [uicolor whitecolor];
    // do any additional setup after loading the view.
    self.botview = [botview new];
    self.topview = [uiview new];
    [self.view addsubview:self.botview];
    [self.botview addsubview:self.topview];
    self.botview.frame = cgrectmake(100, 100, 100, 100);
    self.topview.frame = cgrectmake(0, 0, 50, 200);
    self.botview.backgroundcolor = [uicolor linkcolor];
    self.topview.backgroundcolor = [uicolor orangecolor];
    uitapgesturerecognizer *tap = [[uitapgesturerecognizer alloc]initwithtarget:self action:@selector(topviewclick:)];
    [self.topview addgesturerecognizer:tap];
}
- (void)topviewclick:(uitapgesturerecognizer *)tap {
    nslog(@"点击了顶部view");
}
@end

botview代码

#import "botview.h"
@implementation botview
- (uiview *)hittest:(cgpoint)point withevent:(uievent *)event{
    int count = (int)self.subviews.count;
    for (int i=count-1; i>=0; i--) {
        uiview *subview = self.subviews[i];
        //点击事件作用在子控件上面,返回点击点
        cgpoint ispoint = [self convertpoint:point toview:subview];
        uiview *subv = [subview hittest:ispoint withevent:event];
        if (subv) {
            return subv;
        }
    }
    return self;
}
@end

以上就是ios超出父控件范围无法点击问题解决的详细内容,更多关于ios父控件无法点击的资料请关注代码网其它相关文章!

(0)

相关文章:

版权声明:本文内容由互联网用户贡献,该文观点仅代表作者本人。本站仅提供信息存储服务,不拥有所有权,不承担相关法律责任。 如发现本站有涉嫌抄袭侵权/违法违规的内容, 请发送邮件至 2386932994@qq.com 举报,一经查实将立刻删除。

发表评论

验证码:
Copyright © 2017-2025  代码网 保留所有权利. 粤ICP备2024248653号
站长QQ:2386932994 | 联系邮箱:2386932994@qq.com