比较两个CGRect


95

我需要检查我的视图框架是否等于给定的CGRect。我试图这样做:

CGRect rect = CGRectMake(20, 20, 20, 20);
if (self.view.frame == rect)
{
    // do some stuff
}

但是,我说错了Invalid operands to binary expression('CGRect' (aka 'struct CGRect') and 'CGRect')。为什么我不能简单地比较两个CGRects?

Answers:





2

在Swift中,只需使用==or !=运算符即可:

    let rect = CGRect(x: 0, y: 0, width: 20, height: 20)

    if rect != CGRect(x: 0, y: 0, width: 20, height: 21) {
        print("not equal")
    }

    if rect == CGRect(x: 0, y: 0, width: 20, height: 20) {
        print("equal")
    }

调试控制台打印:

not equal
equal
By using our site, you acknowledge that you have read and understand our Cookie Policy and Privacy Policy.
Licensed under cc by-sa 3.0 with attribution required.