Перемещение UIView относительно касания
Я пытаюсь переместить UIView по отношению к пользователю.
Вот что у меня есть на данный момент:
int oldX, oldY;
BOOL dragging;
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
UITouch *touch = [[event allTouches] anyObject];
CGPoint touchLocation = [touch locationInView:self.view];
if (CGRectContainsPoint(window.frame, touchLocation)) {
dragging = YES;
oldX = touchLocation.x;
oldY = touchLocation.y;
}
}
- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event {
UITouch *touch = [[event allTouches] anyObject];
CGPoint touchLocation = [touch locationInView:self.view];
if (CGRectContainsPoint(window.frame, touchLocation) && dragging) {
CGRect frame;
frame.origin.x = (window.frame.origin.x + touchLocation.x - oldX);
frame.origin.y = (window.frame.origin.y + touchLocation.y - oldY);
window.frame = frame;
}
}
- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event {
dragging = NO;
}
Вид продолжает мерцать из одного места в другое, и я не знаю, что еще делать.
Любая помощь была оценена.
Ответы
Ответ 1
Измените касанияBegan и коснитесьМоделированные методы, как показано ниже.
float oldX, oldY;
BOOL dragging;
ПрикосновенияBegan: withEvent: method.
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
UITouch *touch = [[event allTouches] anyObject];
CGPoint touchLocation = [touch locationInView:self.view];
if (CGRectContainsPoint(window.frame, touchLocation)) {
dragging = YES;
oldX = touchLocation.x;
oldY = touchLocation.y;
}
}
ПрикосновенияMoved: withEvent: method.
- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event {
UITouch *touch = [[event allTouches] anyObject];
CGPoint touchLocation = [touch locationInView:self.view];
if (dragging) {
CGRect frame = window.frame;
frame.origin.x = window.frame.origin.x + touchLocation.x - oldX;
frame.origin.y = window.frame.origin.y + touchLocation.y - oldY;
window.frame = frame;
}
}
Прикосновения: END: withEvent: method.
- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event {
dragging = NO;
}
Ответ 2
Вы хотите использовать UIPanGestureRecognizer
, представленный в iOS 3.2. Вы используете его с чем-то таким же простым, как это (из вашего подкласса UIViewController
):
-(void)viewDidLoad;
{
[super viewDidLoad];
UIPanGestureRecognizer* pgr = [[UIPanGestureRecognizer alloc]
initWithTarget:self
action:@selector(handlePan:)];
[self.panningView addGestureRecognizer:pgr];
[pgr release];
}
-(void)handlePan:(UIPanGestureRecognizer*)pgr;
{
if (pgr.state == UIGestureRecognizerStateChanged) {
CGPoint center = pgr.view.center;
CGPoint translation = [pgr translationInView:pgr.view];
center = CGPointMake(center.x + translation.x,
center.y + translation.y);
pgr.view.center = center;
[pgr setTranslation:CGPointZero inView:pgr.view];
}
}
Ответ 3
Вот код в Swift, немного более универсальный вариант, который работает с ImageView, который был создан на экране.
let panGestureRecongnizer = UIPanGestureRecognizer(target:self, action: "handlepan:")
imageView.addGestureRecognizer(panGestureRecongnizer)
func handlepan(sender: UIPanGestureRecognizer) {
if (sender.state == UIGestureRecognizerState.Changed) {
var center = sender.view?.center
let translation = sender.translationInView(sender.view)
center = CGPointMake(center!.x + translation.x, center!.y + translation.y)
sender.view?.center = center!
sender .setTranslation(CGPointZero, inView: sender.view)
}
}
Ответ 4
//Над ответом в Swift 4.0
перетаскивание: Bool = false
override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
let touch = event?.allTouches?.first
let touchPoint = touch?.location(in: self.window)
i
if viewToDrag.frame.contains(touchPoint!){
dragging = true
}
}
override func touchesMoved(_ touches: Set<UITouch>, with event: UIEvent?) {
let touch = event?.allTouches?.first
let touchPoint = touch?.location(in: self.window)
if (dragging) {
viewToDrag.center = CGPoint(x: (touchPoint?.x)!, y: (touchPoint?.y)!)
}
}
override func touchesEnded(_ touches: Set<UITouch>, with event: UIEvent?) {
dragging = false
}
Ответ 5
@PeyLoW answer - это ответ, который я предлагаю, и добавьте ограничение к родительским границам
-(void)handlePan:(UIPanGestureRecognizer*)pgr;
{
if (pgr.state == UIGestureRecognizerStateChanged) {
CGPoint center = pgr.view.center;
CGPoint translation = [pgr translationInView:pgr.view];
center = CGPointMake(center.x + translation.x,
center.y + translation.y);
if ([self pointInside:center withEvent:nil])
{
pgr.view.center = center;
[pgr setTranslation:CGPointZero inView:pgr.view];
}
}
}