2011-11-10 7 views

Respuesta

77

supongo que busca este método

– convertRect:toView:

// Swift 
let frame = imageView.convert(button.frame, to: self.view) 

// Objective-C 
CGRect frame = [imageView convertRect:button.frame toView:self.view]; 
2

¿Algo como esto? podría estar totalmente equivocado, a fuerza de verdad a través de thinkt; p

CGRect frame = CGRectMake((self.view.frame.origin.x-imageview.frame.origin.x) +btn.frame.origin.x, 
          (self.view.frame.origin.y.imageview.frame.origin.y)+btn.frame.origin.y, 
          btn.frame.size.width, 
          btn.frame.size.height); 

No sé si tener manera más fácil.

17

Hay cuatro UIView métodos que pueden ayudarle, convirtiendo CGPoints y CGRects de un UIView de coordenadas de referencia a otro:

– convertPoint:toView: 
– convertPoint:fromView: 
– convertRect:toView: 
– convertRect:fromView: 

para que pueda probar

CGRect f = [imageView convertRect:button.frame toView:self.view]; 

o

CGRect f = [self.view convertRect:button.frame fromView:imageView]; 
7

Swift 3

Puede convertir el marco del botón para el sistema de coordenadas del punto de vista con esto:

self.view.convert(myButton.frame, from: myButton.superview)


Asegúrese de poner su lógica dentro de viewDidLayoutSubviews y no viewDidLoad. Las operaciones relacionadas con la geometría deben realizarse después de que se hayan presentado las subvistas, de lo contrario, es posible que no funcionen correctamente.

class ViewController: UIViewController { 

    @IBOutlet weak var myImageView: UIImageView! 
    @IBOutlet weak var myButton: UIButton! 

    override func viewDidLayoutSubviews() { 
     super.viewDidLayoutSubviews() 

     let buttonFrame = self.view.convert(myButton.frame, from: myButton.superview) 
    } 
} 

Usted sólo puede hacer referencia a myButton.superview en lugar de myImageView al convertir el marco.


Aquí hay más opciones para convertir un CGPoint o CGRect.

self.view.convert(point: CGPoint, from: UICoordinateSpace) 
self.view.convert(point: CGPoint, from: UIView)    
self.view.convert(rect: CGRect, from: UICoordinateSpace) 
self.view.convert(rect: CGRect, from: UIView) 

self.view.convert(point: CGPoint, to: UICoordinateSpace) 
self.view.convert(point: CGPoint, to: UIView) 
self.view.convert(rect: CGRect, to: UICoordinateSpace) 
self.view.convert(rect: CGRect, to: UIView) 

Véase el Apple Developer Docs para más información sobre la conversión de un CGPoint o CGRect.

Cuestiones relacionadas