2011-11-14 51 views
0

我在自定義UIView中創建了200x200圓。我正在使用以下腳本在iPad上的屏幕上移動視圖對象。防止視圖對象在用手指觸摸移動時跳躍

- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event 
{  
UITouch *touch = [touches anyObject]; 
CGPoint currentPoint = [touch locationInView:self.view]; 

if([touch view] == newShape) 
{ 
    newShape.center = currentPoint; 
} 

[self.view setNeedsDisplay]; 
} 

一切工作正常,我可以移動屏幕上任何地方的圓圈。但是,如果我沒有碰到圓形物體的死點,它會略微跳躍。通過閱讀代碼,這是非常明顯的,因爲newShape.center被設置到觸摸發生的任何地方,並最終迅速捕捉到該位置。

我正在尋找一種方法來移動物體而不會捕捉到觸摸位置。我想我會使用xy座標來實現這一點,但我不知道如何實現它。

謝謝!

+0

關閉我的頭頂(沒有代碼,在iPhone上)我想一個簡單的if-then要弄清楚你的手指是否不是死點。只需在[UIView]動畫塊中使用newShape.center行即可。 (和認真,腳本LOL) – CodaFi

回答

2

在.h文件中聲明CGPoint prevPos;

這裏我的看法是_rectView

-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event 
{ 
    UITouch *touch = [touches anyObject]; 
    CGPoint currentPoint = [touch locationInView:self.view]; 
    prevPos = currentPoint; 
} 

-(void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event 
{ 
    UITouch *touch = [touches anyObject]; 
    CGPoint currentPoint = [touch locationInView:self.view]; 

    if([touch view] == _rectView) 
    { 
     float delX = currentPoint.x - prevPos.x; 
     float delY = currentPoint.y - prevPos.y; 
     CGPoint np = CGPointMake(_rectView.frame.origin.x+delX, _rectView.frame.origin.y+delY); 
     //_rect.center = np; 
     CGRect fr = _rectView.frame; 
     fr.origin = np; 
     _rectView.frame = fr; 
    } 

    //[self.view setNeedsDisplay]; 

    prevPos = currentPoint; 
} 

使用上面的代碼。你不會得到那種「跳躍」效應。

+0

我的答案解決了你的問題嗎? – Ilanchezhian

+0

我有一種感覺,它與'frame'屬性有關,但我不知道如何實現它。我會在今晚嘗試代碼並標記你的答案。謝謝。 – anthony

+0

工作完美!我遺漏了最後一行,它變得有點瘋狂,但是一旦我添加它,它就起作用了。謝謝! – anthony

0

一個明顯的方法是將形狀中心的觸摸偏移量存儲在-touchesBegan:withEvent:中,並將偏移量應用於-touchesMoved:withEvent: