2014-11-13 10 views

risposta

27

Puoi utilizzare CGRectInset se si desidera:

Per ridurre le dimensioni, rimuovere - s.

Nota a margine: Un CGRect che è del 20% più grande di {10, 10, 100, 100} è {0, 0, 120, 120}.


Edit: Se l'intenzione è quella di aumentare di zona, allora questo lo farà (anche per i rettangoli che non sono quadrati):

CGFloat width = CGRectGetWidth(oldRect); 
CGFloat height = CGRectGetHeight(oldRect); 
double pct = 1.2; // 20% increase 
double newWidth = sqrt(width * width * pct); 
double newHeight = sqrt(height * height * pct); 
CGRect newRect = CGRectInset(oldRect, (width-newWidth)/2, (height-newHeight)/2); 
+0

Re sidenote: Penso che sia corretto che (110,110) dia un'area del 20% più grande di (100,100). – thelaws

+1

Suppongo che dipenda dalla tua interpretazione di "20% più grande". Per area, il quadrato risultante ha bordi di lunghezza '109,5445', che non sono così belli come se si pensasse che avrebbe aumentato le dimensioni del bordo del 20%. –

+1

Intendevo aumentare le dimensioni dei bordi, ma hai ragione, l'area sarebbe un calcolo completamente diverso, e questa è una buona risposta. – brandonscript

4

Certo, utilizzando CGRectInset opere:

CGRect someRect = CGRectMake(10, 10, 100, 100); 
someRect = CGRectInset(someRect, someRect.size.width * -0.2, someRect.size.height * -0.2); 
8

In Swift:

func increaseRect(rect: CGRect, byPercentage percentage: CGFloat) -> CGRect { 
    let startWidth = CGRectGetWidth(rect) 
    let startHeight = CGRectGetHeight(rect) 
    let adjustmentWidth = (startWidth * percentage)/2.0 
    let adjustmentHeight = (startHeight * percentage)/2.0 
    return CGRectInset(rect, -adjustmentWidth, -adjustmentHeight) 
} 

let rect = CGRectMake(0, 0, 10, 10) 
let adjusted = increaseRect(rect, byPercentage: 0.1) 
// -0.5, -0.5, 11, 11 

In objC:

- (CGRect)increaseRect:(CGRect)rect byPercentage:(CGFloat)percentage 
{ 
    CGFloat startWidth = CGRectGetWidth(rect); 
    CGFloat startHeight = CGRectGetHeight(rect); 
    CGFloat adjustmentWidth = (startWidth * percentage)/2.0; 
    CGFloat adjustmentHeight = (startHeight * percentage)/2.0; 
    return CGRectInset(rect, -adjustmentWidth, -adjustmentHeight); 
} 

CGRect rect = CGRectMake(0,0,10,10); 
CGRect adjusted = [self increaseRect:rect byPercentage:0.1]; 
// -0.5, -0.5, 11, 11 
+1

La domanda è etichettata Objective-C. Meglio dare risposte nella lingua desiderata. – rmaddy

+0

@rmaddy - L'ho aggiunto anch'io, grazie! – Logan

+2

+1 per essere l'unico responsabile fino ad ora ad aver obbedito alla dichiarazione di Apple secondo cui "le tue applicazioni dovrebbero evitare di leggere e scrivere direttamente i dati archiviati nella struttura dati di CGRect, ma usa le funzioni qui descritte per manipolare i rettangoli e recuperare le loro caratteristiche . "_ - https://developer.apple.it/Libreria/ios/documentazione/GraphicsImaging/Reference/CGGeometry/index.html – Tommy

1

Usando Swift è possibile mantenere il punto centrale (rispetto alla sorgente Rect) e di aumentare/diminuire il formato come segue utilizzando un'estensione:

extension CGRect { 
    func centerAndAdjustPercentage(percentage p: CGFloat) -> CGRect { 
     let x = self.origin.x 
     let y = self.origin.y 
     let w = self.width 
     let h = self.height 

     let newW = w * p 
     let newH = h * p 
     let newX = (w - newW)/2 
     let newY = (h - newH)/2 

     return CGRect(x: newX, y: newY, width: newW, height: newH) 
    } 
} 
let newRect = oldRect.centerAndAdjustPercentage(percentage: 0.25) 
2
let scale = percent/100 
let newRect = oldRect.applying(CGAffineTransform(scaleX: scale, y: scale)) 

Attenzione che questo approccio cambierà anche xey del rettangolo.

+0

Questo non sembra scalare rispetto al centro del rettangolo, quindi non cambia l'origine x, y. – lhunath

Problemi correlati