簡體   English   中英

iphone拖放怪異的跳動

[英]iphone drag and drop weird jumpy movement

目前,我正在為我的iPad應用程序構建交互式字謎功能,並嘗試使用touchmove事件來使所選項目平穩地跟隨運動,但是出於某些奇怪的原因,它正在跳躍。

-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event{
    UITouch *touch = [touches anyObject];
    NSString *touchClass = [NSString stringWithFormat:@"%@",[[touch view] class]];

    if ([touchClass isEqualToString:[NSString stringWithFormat:@"%@",[AnagramLetter class]]]) {
        NSLog(@"start moving");
    }
}

-(void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event {
    UITouch *touch = [touches anyObject];
    NSString *touchClass = [NSString stringWithFormat:@"%@",[[touch view] class]];
    CGPoint location = [touch locationInView:touch.view];
    if ([touchClass isEqualToString:[NSString stringWithFormat:@"%@",[AnagramLetter class]]]) {
        NSLog(@"touch view centre (x,y) - (%f,%f)",touch.view.center.x,touch.view.center.y);
        NSLog(@"location (x,y) - (%f,%f)",location.x,location.y);
        touch.view.center = location;
    }
}

-(void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event{
    UITouch *touch = [touches anyObject];
    NSString *touchClass = [NSString stringWithFormat:@"%@",[[touch view] class]];

    if ([touchClass isEqualToString:[NSString stringWithFormat:@"%@",[AnagramLetter class]]]){
        NSLog(@"Now stop moving!");
    }
}

上面是我的3種觸控方法,看來它們應該工作正常。 您可以在touchesMoved方法中看到正在檢查的對象是否屬於特定類(我為同一項目創建的自定義類)。 有誰知道為什么會這樣? 它跟隨模擬器中的光標,但是在跟隨光標之前它似乎捕捉到了底角,因此它基本上在整個屏幕上跳躍。

有任何想法嗎?

我相信問題是您正在使用相對於要觸摸的子視圖的觸摸。

[touch locationInView:[touch.view superview]];

將為您提供一個位置,以引用您的自定義視圖的父級。

在touchesMoved方法中,您可以查看位移並將相同的位移應用於視圖

if ([touchClass isEqualToString:[NSString stringWithFormat:@"%@",[AnagramLetter class]]]) {
        UITouch * touch = [touches anyObject];
        CGPoint previous = [touch previousLocationInView:[touch.view superview]];
        CGPoint current = [touch locationInView:[touch.view superview]];
        CGPoint displacement = CGPointMake(current.x - previous.x, current.y - previous.y);

        CGPoint center = touch.view.center;
        center.x += displacement.x;
        center.y += displacement.y;
        touch.view.center = center;
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM