2010-01-02 115 views
8

相當普遍的問題,我有幾個答案,我幾乎在那裏。我有一個按鈕按下時,將(如下代碼)創建圖像UIImage檢測觸摸和拖動

(numImages上負載設置爲零,並且被用作計數開創建的所有圖像的標籤號)

UIImage *tmpImage = [[UIImage imageNamed:[NSString stringWithFormat:@"%i.png", sender.tag]] retain]; 
UIImageView *myImage = [[UIImageView alloc] initWithImage:tmpImage]; 

numImages += 1; 

myImage.userInteractionEnabled = YES; 
myImage.tag = numImages; 
myImage.opaque = YES; 
[self.view addSubview:myImage]; 
[myImage release]; 

然後我有一個touchesBegan方法,它會檢測到什麼被觸摸。我需要做的是讓用戶拖動新創建的圖像。它幾乎可以工作,但拖動它時,圖像會在整個地方閃爍。我可以訪問你點擊的圖像,因爲我可以得到它的標籤,但我不能很好地拖動它。

- (void) touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event { 

    UITouch *touch = [[event allTouches] anyObject]; 
    CGPoint location = [touch locationInView:touch.view]; 

    if (touch.view.tag > 0) { 
     touch.view.center = location; 
    } 

    NSLog(@"tag=%@", [NSString stringWithFormat:@"%i", touch.view.tag]); 

} 

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

它的工作原理是,當我點擊它們時,我得到了每個圖像的標籤輸出。但是當我拖動時,它閃爍......任何想法?

回答

34

回答我自己的問題 - 我決定創建一個類來處理我放置在視圖上的圖像。

代碼,如果任何人的興趣....

Draggable.h

#import <Foundation/Foundation.h> 
@interface Draggable : UIImageView { 
    CGPoint startLocation; 
} 
@end 

Draggable.m

#import "Draggable.h" 
@implementation Draggable 

- (void) touchesBegan:(NSSet*)touches withEvent:(UIEvent*)event { 
    // Retrieve the touch point 
    CGPoint pt = [[touches anyObject] locationInView:self]; 
    startLocation = pt; 
    [[self superview] bringSubviewToFront:self]; 
} 
- (void) touchesMoved:(NSSet*)touches withEvent:(UIEvent*)event { 
    // Move relative to the original touch point 
    CGPoint pt = [[touches anyObject] locationInView:self]; 
    CGRect frame = [self frame]; 
    frame.origin.x += pt.x - startLocation.x; 
    frame.origin.y += pt.y - startLocation.y; 
    [self setFrame:frame]; 
} 
@end 

,並把它稱爲

UIImage *tmpImage = [[UIImage imageNamed:"test.png"] retain]; 
UIImageView *imageView = [[UIImageView alloc] initWithImage:tmpImage]; 

CGRect cellRectangle; 
cellRectangle = CGRectMake(0,0,tmpImage.size.width ,tmpImage.size.height); 
UIImageView *dragger = [[Draggable alloc] initWithFrame:cellRectangle]; 
[dragger setImage:tmpImage]; 
[dragger setUserInteractionEnabled:YES]; 

[self.view addSubview:dragger]; 
[imageView release]; 
[tmpImage release]; 
+4

上面的代碼來自http://www.iphoneexamples.com/ – 2010-01-02 19:43:20

+0

感謝張貼這個,但我得到抖動的效果,而這樣做,它可以如何避免? – itsaboutcode 2012-04-26 00:43:02

+0

謝謝你!像魅力一樣工作:) – soupdiver 2013-08-28 10:17:58

1

當您更改center時,通常會顯示隱式動畫。你有沒有搞亂-contentMode或打電話-setNeedsDisplay

您可以明確要求的動畫,以避免刪除和重新繪製這樣:

if (touch.view.tag > 0) { 
    [UIView beginAnimations:@"viewMove" context:touch.view]; 
    touch.view.center = location; 
    [UIView commitAnimations]; 
} 

請注意NSLog()可能會很慢(遠遠低於你所期望的,它比一個更復雜簡單的printf),並且這可能會導致經常出現的問題,如touchesMoved:withEvent:

順便說一句,你漏水tmpImage

+0

感謝您的答覆。我試過你的建議,但它仍然發生。我認爲這與我用來設置圖像的代碼有關?當我在Interface Builder中的視圖上放置圖像並將其設置爲.h文件中的IBOutlet UIImageView時,該圖像完美地移動。我在設置每張圖像時是否錯過了一些東西? (ps)感謝tmpImage提示 - 錯過了那一個! – 2010-01-02 17:15:10

+0

我認爲這可能是一個問題與標籤..但現在我認爲這可能是一個問題與意見。當我使用第一位代碼放置圖像時,它會變爲0,0。當我拖動它時,它似乎總想回到那裏。所以這是導致閃爍。當前的位置,然後它回到那裏......它驅使我瘋狂! – 2010-01-02 17:50:47