Ich muss mein UIView-Objekt ziehen. Ich benutze diesen Code, aber es funktioniert nicht
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;
}
}
}
In Ihrem Code ist etwas seltsam.
Sie fragen nach dem Ort in self
, einem Teil von UIView, der vermutlich die zu überprüfende UILabel
enthält. Dies wirft die Frage auf, warum Sie die touchesXXX nicht zu einer Ihrer UILabel-Unterklassen hinzufügen.
Dies wird abgebrochen, wenn Sie den label.frame verwenden, der in seinen superview.bounds definiert ist (Ihr übergeordneter UIView, den Sie nach dem Ort der Berührung gefragt haben). Dies ist jedoch nicht der einfachste Weg, dem zu folgen los.
Ich würde vorschlagen, eine UIPanGestureRecognizer
zu verwenden:
-(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);
}
Dies ist nur eine Präferenz von mir. Für mich ist es viel einfacher, eine Gestenerkennung zu verwenden als touchesBegan
, touchesEnded
, touchesMoved
. Ich werde diese in Fällen verwenden, in denen die UIPanGestureRecognizer
nicht funktioniert.
Dies ist der Arbeitscode zum Verschieben von UIView-Objekten
float oldX, oldY; BOOL ziehen;
- (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;
}
}
}
Mit diesem Code könnte ich mein UIView-Objekt an einen beliebigen Ort verschieben. Mein ViewController.Swift sieht so aus.
// 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
Hoffe, es hilft, obwohl es eine andere Möglichkeit ist, als dies in Frage zu stellen.