您好,欢迎来到二三四教育网。
搜索
您的当前位置:首页CGRect相关(未完待续...)

CGRect相关(未完待续...)

来源:二三四教育网

1、CGRectInset
CGRect CGRectOffset(CGRect rect, CGFloat dx, CGFloat dy)是以rect为中心,根据dx和dy来实现缩小。
可以理解为,高度和宽度相对于view1分别缩小20像素。正值表示缩小,负值表示扩大。
2.CGRectOffset
CGRect CGRectOffset(CGRect rect, CGFloat dx, CGFloat dy)
它是以rect左上角为基点,向X轴和Y轴偏移dx和dy像素。
3.CGRectContainsRect
CGRectContainsRect(CGRect rect1, CGRect rect2)
判断两个结构体是否有交错

  1. CGRectContainsPoint
    CGRectContainsRect(CGRect rect, CGPoint point)
    判断一个点是否包含在矩形中,所以参数为一个点一个矩形
    其中 CGSize和CGPoint的比较方法如出一辙

演练:增加按钮响应区域

-(UIView *)hitTest:(CGPoint)point withEvent:(UIEvent *)event {
    if (!self.isUserInteractionEnabled || self.hidden || self.alpha<=0.01) {
        return nil;
    }
    CGRect touchRect = CGRectInset(self.bounds, -20, -20);
    if (CGRectContainsPoint(touchRect, point)) {
        for (UIView *subView in [self.subviews reverseObjectEnumerator]) {
            CGPoint convertedPoint = [subView convertPoint:point toView:self];
            UIView *hitTestView = [subView hitTest:convertedPoint withEvent:event];
            if (hitTestView) {
                return hitTestView;
            }
        }
        return self;
    }
    return nil;
}

UIKit提供了一下几种坐标转换的方法:

  • (CGPoint)convertPoint:(CGPoint)point toView:(nullable UIView *)view;
  • (CGPoint)convertPoint:(CGPoint)point fromView:(nullable UIView *)view;
  • (CGRect)convertRect:(CGRect)rect toView:(nullable UIView *)view;
  • (CGRect)convertRect:(CGRect)rect fromView:(nullable UIView *)view;

Copyright © 2019- how234.cn 版权所有 赣ICP备2023008801号-2

违法及侵权请联系:TEL:199 1889 7713 E-MAIL:2724546146@qq.com

本站由北京市万商天勤律师事务所王兴未律师提供法律服务