ios objective-c xcode uiview touchesbegan

ios - toques Empiezo, toques Terminado, toques Movido para mover UIView



objective-c xcode (4)

Con este código podría mover mi objeto UIView a cualquier lugar. Mi ViewController.swift se ve así.

// code from below import UIKit class ViewController: UIViewController { var location = CGPoint(x: 0, y: 0) @IBOutlet weak var Person: UIImageView! override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) { var touch : UITouch! = touches.first! as UITouch location = touch.location(in: self.view) Person.center = location } override func touchesMoved(_ touches: Set<UITouch>, with event: UIEvent?) { var touch : UITouch! = touches.first! as UITouch location = touch.location(in: self.view) Person.center = location } override func viewDidLoad() { super.viewDidLoad() // Do any additional setup after loading the view, typically from a nib. Person.center = CGPoint(x: 160, y: 330) } override func didReceiveMemoryWarning() { super.didReceiveMemoryWarning() // Dispose of any resources that can be recreated. } } //ends

Espero que ayude, aunque es otra forma de hacerlo que la mencionada en la pregunta.

Necesito arrastrar mi objeto UIView. Yo uso este código, pero no funciona

float oldX, oldY; BOOL dragging; - (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event { UITouch *touch = [[event allTouches] anyObject]; CGPoint touchLocation = [touch locationInView:self]; if ([[touch.view class] isSubclassOfClass:[UILabel class]]) { UILabel *label = (UILabel *)touch.view; if (CGRectContainsPoint(label.frame, touchLocation)) { dragging = YES; oldX = touchLocation.x; oldY = touchLocation.y; } } } - (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event { dragging = NO; } - (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event { UITouch *touch = [[event allTouches] anyObject]; CGPoint touchLocation = [touch locationInView:self]; if ([[touch.view class] isSubclassOfClass:[UILabel class]]) { UILabel *label = (UILabel *)touch.view; if (dragging) { CGRect frame = label.frame; frame.origin.x = label.frame.origin.x + touchLocation.x - oldX; frame.origin.y = label.frame.origin.y + touchLocation.y - oldY; label.frame = frame; } } }


Este es el código de trabajo para mover objetos UIView

float oldX, oldY; BOOL arrastrando;

- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event { UITouch *touch = [[event allTouches] anyObject]; CGPoint touchLocation = [touch locationInView:touch.view]; if ([[touch.view class] isSubclassOfClass:[UILabel class]]) { dragging = YES; oldX = touchLocation.x; oldY = touchLocation.y; } } - (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event { dragging = NO; } - (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event { UITouch *touch = [[event allTouches] anyObject]; CGPoint touchLocation = [touch locationInView:touch.view]; if ([[touch.view class] isSubclassOfClass:[UILabel class]]) { UILabel *label = (UILabel *)touch.view; if (dragging) { CGRect frame = label.frame; frame.origin.x = label.frame.origin.x + touchLocation.x - oldX; frame.origin.y = label.frame.origin.y + touchLocation.y - oldY; label.frame = frame; } } }


Hay algo un poco extraño en tu código.

Usted solicita la ubicación en self , algunas UIView que presumiblemente contienen la UILabel que desea verificar. Esto plantea la pregunta de por qué no agrega el touchesXXX a alguna subclase de UILabel suya.

Esto se cancela a medida que usa el label.frame que se define en términos de superview.bounds (su UIView principal a la que solicitó la ubicación de contacto), pero esto no es la forma más sencilla de seguir lo que es continuando


Yo sugeriría usar un UIPanGestureRecognizer :

-(void)dragging:(UIPanGestureRecognizer *)gesture { // Check if this is the first touch if(gesture.state == UIGestureRecognizerStateBegan) { // Store the initial touch so when we change positions we do not snap self.panCoord = [gesture locationInView:gesture.view]; [self.view bringSubviewToFront:gesture.view]; } CGPoint newCoord = [gesture locationInView:gesture.view]; // Create the frame offsets to use our finger position in the view. float dX = newCoord.x-self.panCoord.x; float dY = newCoord.y-self.panCoord.y; gesture.view.frame = CGRectMake(gesture.view.frame.origin.x+dX, gesture.view.frame.origin.y+dY, gesture.view.frame.size.width, gesture.view.frame.size.height); }

Esto es solo una preferencia mía. Para mí es mucho más sencillo usar un reconocedor de gestos y luego usar touchesBegan , touchesEnded , touchesMoved . Los UIPanGestureRecognizer en los casos en los que UIPanGestureRecognizer no funcionará.