2011-04-15 47 views
0

我得到了一個名爲IconView的UIImageView圖標。我想確保何時觸摸並移動此圖標,其位置僅在另一個名爲backgroundView的UIImageView的邊界內發生變化。如何限制UIImageView在邊界範圍內的位置?

我以爲我添加IconView作爲backgroundView的子視圖後,邊界會自動設置。但它似乎是錯誤的。

[backgroundView addSubview:IconView];

之後,我仍然可以將圖標移動到backgroundView的外部。 我該如何設置限制?謝謝。

回答

0

你試試這樣,

- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event { 
    UITouch *touch = [[event allTouches] anyObject]; 

    CGPoint location = [touch locationInView:self.view]; 


    if(CGRectContainsPoint(urBoundryBackgroundImage.frame, location)) { 
     //do your moving stuff    
    } 
} 
+0

它的工作原理!非常感謝! – Nauhc 2011-04-15 08:42:35