2011-07-20 28 views
1

如何根據用戶在視圖中觸摸的點或移動點,非常快速地移除子視圖。我能夠獲取用戶觸摸點並移動視圖中的點,也可以刪除子視圖根據用戶觸摸的點使用[subview removefromsuperview] ;.當用戶移動速度非常快時,從超級視圖中刪除子視圖

當用戶移動速度非常快一些子視圖得到刪除,一些沒有得到deleted.If用戶圖像的移動速度很慢,越來越子視圖刪除exactly.i我把我的一些代碼如下

`

-(void)deleteSubView:(CGPoint)userCurrentPoint{ 

for(int i=0;i<[subviews count];i++){ 

if (CGRectContainsPoint([[subviews objectAtIndex:i] CGRectValue], userCurrentPoint)) { 

       [[superview viewWithTag:i ] removeFromSuperview]; 



    } 
} 

`

有什麼辦法快速移動points.i需要你的建議用戶根據刪除子視圖。 感謝所有

+0

可能是快速枚舉可以幫助你...雖然它似乎並不合邏輯.. – Suny

+0

它也是相同的ntg很大的區別.. – ajay

回答

2

在你的頭文件將這個:

@interface YourView : UIView 

@property (nonatomic, assign) CGPoint lastTouchLocation; 
@end 

這應該是執行:

@implementation YourView 
@synthesize lastTouchLocation; 

- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event 
{ 
    [super touchesBegan:touches withEvent:event]; 

    self.lastTouchLocation = [[touches anyObject] locationInView:self]; 
} 

- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event 
{ 
    [super touchesMoved:touches withEvent:event]; 

    CGPoint oldLocation = self.lastTouchLocation; 
    CGPoint newLocation = [[touches anyObject] locationInView:self]; 

    for (UIView *subview in self.subviews) 
    { 
     if (LineIntersectsRect(oldLocation, newLocation, subview.frame)) 
     { 
      [subview removeFromSuperview]; 
     } 
    } 

    self.lastTouchLocation = newLocation; 
} 

- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event 
{ 
    [super touchesEnded:touches withEvent:event]; 

    self.lastTouchLocation = CGPointZero; 
} 

- (void)touchesCancelled:(NSSet *)touches withEvent:(UIEvent *)event 
{ 
    [super touchesCancelled:touches withEvent:event]; 

    self.lastTouchLocation = CGPointZero; 
} 
@end 

這裏做的事情:我們檢查上次之間的線路是否將接收到觸摸和電流一個與子視圖的框架相交。爲什麼?見deanWombourne的答案。

這裏是十字路口的代碼,原來是habjan - 你也應該把頭部放在頭部。

static inline BOOL LineIntersectsLine(CGPoint l1p1, CGPoint l1p2, CGPoint l2p1, CGPoint l2p2) 
{ 
    CGFloat q = (l1p1.y - l2p1.y) * (l2p2.x - l2p1.x) - (l1p1.x - l2p1.x) * (l2p2.y - l2p1.y); 
    CGFloat d = (l1p2.x - l1p1.x) * (l2p2.y - l2p1.y) - (l1p2.y - l1p1.y) * (l2p2.x - l2p1.x); 

    if(d == 0) 
    { 
     return false; 
    } 

    CGFloat r = (q/d); 
    q = (l1p1.y - l2p1.y) * (l1p2.x - l1p1.x) - (l1p1.x - l2p1.x) * (l1p2.y - l1p1.y); 

    CGFloat s = (q/d); 

    if((r < 0) || (r > 1) || (s < 0) || (s > 1)) 
    { 
     return false; 
    } 
    else 
    { 
     return true; 
    } 
} 

static inline BOOL LineIntersectsRect(CGPoint p1, CGPoint p2, CGRect r) 
{ 
    if (CGRectContainsPoint(r, p1) || CGRectContainsPoint(r, p2)) 
    { 
     return YES; 
    } 
    else 
    { 
     CGPoint topLeft = CGPointMake(r.origin.x, r.origin.y); 
     CGPoint topRight = CGPointMake(r.origin.x + r.size.width, r.origin.y); 
     CGPoint bottomLeft = CGPointMake(r.origin.x, r.origin.y + r.size.height); 
     CGPoint bottomRight = CGPointMake(r.origin.x + r.size.width, r.origin.y + r.size.height); 

     return (LineIntersectsLine(p1, p2, topLeft, topRight) || 
       LineIntersectsLine(p1, p2, topRight, bottomRight) || 
       LineIntersectsLine(p1, p2, bottomRight, bottomLeft) || 
       LineIntersectsLine(p1, p2, bottomLeft, topLeft)); 
    } 
} 
+0

jenox謝謝你的回答我會檢查你的寫法.. – ajay

+0

sry jenox上面的代碼不適合我..我正在尋找其他人 – ajay

+0

你可能會發布你的項目的壓縮版本?我會爲你工作。 ;) –

1

這不是你的枚舉:)

做如果用戶快速移動手指,你沒有得到他們碰到的每一個像素的名單,你會得到他們的一些。如果他們的移動速度夠快,你可能只會得到開始和結束點。

你不能只用自己的手指拖動來,你得把這些子視圖已經過去的事件之間的接觸點,這一個:)

可以把它當作繪畫之間的線開始和結束點,並確定哪些子視圖與該行重疊。

+0

哈哈這正是我所做的。但你快了一秒!該死的。 –

+0

更快的真實,但你設法得到真正的代碼™到你的答案 - 我只是讓他自己解決它:) – deanWombourne

+0

我想告訴他如何檢測這樣一個直線 - 交叉 - 這真的很複雜沒有好的代碼! :D –

0

如果我正確理解您的問題,您希望刪除用戶在您的視圖中拖動手指時觸及的所有子視圖。

你的問題是touchesMoved:withEvent:以有限的頻率點火。不保證爲用戶觸摸的每個像素點亮。因此,當用戶快速移動他的手指時,在touchesMoved:withEvent中報告的點之間存在間隙。如果用戶足夠快地移動他的手指,這些間隙可能足夠大,以至於完全跳過一些子視圖。

爲了解決這個問題,你的代碼需要記住前一個點的位置,然後測試前一個點和當前點形成的線段,看它是否與子視圖的框架相交。一個有效的算法描述如下:How to test if a line segment intersects an axis-aligned rectange in 2D?

相關問題