簡體   English   中英

iPhone SDK:麻煩拖動UIImageView

[英]iPhone SDK:Trouble Dragging a UIImageView

我試圖在我的應用程序中圍繞iphone屏幕拖動UIImageView。

目前我設置的拖動功能很好,拖動圖像確實會在屏幕上移動它,麻煩的是你不必拖動圖像視圖來移動它,你也可以拖動到屏幕上的任何地方它會移動圖像。 我是這個平台的新手,所以我無法真正思考如何解決這個問題。 我當前拖動圖片的代碼是:

 - (void) touchesBegan:(NSSet*)touches withEvent:(UIEvent*)event {
    CGPoint pt = [[touches anyObject] locationInView:pig];
    startLocation = pt;


}
- (void) touchesMoved:(NSSet*)touches withEvent:(UIEvent*)event {
    CGPoint pt = [[touches anyObject] locationInView:pig];
    CGRect frame = [pig frame];
    frame.origin.x += pt.x - startLocation.x;
    frame.origin.y += pt.y - startLocation.y;
    [pig setFrame: frame];
}

任何幫助贊賞。 順便說一下,豬是UIImageView。 另外我注意到,如果我將圖像視圖“pig”的用戶交互設置為啟用,則圖像不再可拖動,但是當它未設置為啟用時它是。

您需要至少在計時器線程中將它包含在begin / commit動畫序列中:

[UIView beginAnimations:nil context:NULL];
[UIView setAnimationDuration:0.02]  /* 0.02 seconds suration*/

/* move pig coordinates here */

[UIView commitAnimations];

將新移動保存在touchesMoved中,然后在計時器線程中處理它們。

您可以嘗試子類化UIImageView並直接在其上實現touchesBegan:touchesMoved: . 然后在InterfaceBuilder中選擇UIImageView的子類。

替代文字http://img.skitch.com/20090728-xetretcfubtcj7ujh1yc8165wj.jpg

哦,是的,現在你需要再次設置用戶交互......

您可以使用UIView類別使其可拖動並定義可拖動區域: https//github.com/rtoshiro/TXDragAndDrop

暫無
暫無

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

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