我有一個應用程序,用戶點擊一個按鈕,應用程序將實例化一個圖像層(UIImageView)。我想要讓用戶可以移動選定的圖像層,無論他們點擊哪一個。在閱讀了一些主題後,我瞭解到我可以使用UIPanGestureRecognizer來移動選定的圖像層。UIPanGestureRecognizer和BringSubviewToFront
- (IBAction)buttonClicked:(id)sender {
imageview = [[UIImageView alloc] initWithFrame:CGRectMake(100, 0, 300, 22)];
UIPanGestureRecognizer *imageviewGesture = [[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(recognizePan:)];
[imageviewGesture setMinimumNumberOfTouches:1];
[imageviewGesture setMaximumNumberOfTouches:1];
imageview.image = [UIImage imageNamed:@"pinimage.png"];
[imageview addGestureRecognizer:imageviewGesture];
[imageview setUserInteractionEnabled:YES];
[self.view addSubview:imageview];
NSUInteger currentag = [self UniqueTag]; // Assigning a unique integer
imageview.tag = currentag;
}
- (void)recognizePan:(UIPanGestureRecognizer *)sender {
[[[(UITapGestureRecognizer *)sender view] layer] removeAllAnimations];
[self.view bringSubviewToFront:[(UIPanGestureRecognizer *)sender view]];
CGPoint translatedPoint = [(UIPanGestureRecognizer *)sender translationInView:self.view];
if([(UIPanGestureRecognizer *)sender state] == UIGestureRecognizerStateBegan) {
firstX = [[sender view] center].x;
firstY = [[sender view] center].y;
}
translatedPoint = CGPointMake(firstX + translatedPoint.x,firstY + translatedPoint.y);
[[sender view] setCenter:translatedPoint];
}
現在,我無法弄清楚如何在有多個圖像層時將挖掘圖層放在前面。它似乎沒有[self.view bringSubviewToFront:[(UIPanGestureRecognizer *)發送者視圖]]有效。那麼,我該如何修改我的代碼,以便應用程序將觸發層添加到其他層?
謝謝你的幫助。
謝謝。但是,你真的和我的代碼有什麼不同嗎? –
@TBlue,你說得很對,我沒有直接想......但這仍然存在問題,你爲什麼不工作?我的工作,它基本上做和你一樣的工作... – foundry
@TBlue - 我發現你的錯誤,看到我的更新... – foundry