2012-10-13 26 views
85

Avevo bisogno di controllare se il frame della mia vista è uguale a un dato CGRect. Ho provato a fare che in questo modo:Confronto di due CGRect

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

Tuttavia, ho ottenuto un errore che dice Invalid operands to binary expression('CGRect' (aka 'struct CGRect') and 'CGRect'). Perché non posso semplicemente confrontare due sCGRect s?

risposta

217

Utilizzare questa:

if (CGRectEqualToRect(self.view.frame, rect)) { 
    // do some stuff 
} 
+7

+1 per risposta più concreta –

+0

1 come sopra. – ManicMonkOnMac

39

Vedere the documentation per CGRectEqualToRect().

bool CGRectEqualToRect (CGRect rect1, CGRect rect2); 
4

Nel Swift 3 sarebbe:

frame1.equalTo(frame2) 
+1

O usando il buon vecchio operatore "==". –