2010-01-02 4 views
8

상당히 일반적인 질문이, 내가 몇 가지 답변을 가지고 끌어와 내가 거의 다입니다. I 누를 때 (이하 코드)에 의해, 화상을 생성하는 버튼을이있는 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/에서이 게시 –

+0

감사를 제공하지만,이 일을하는 동안 나는 그것을 피할 수있는 방법, 지터 효과는 무엇입니까? – itsaboutcode

+0

고마워요! 매력처럼 작동 :) – soupdiver

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

감사를 호출하기 . 나는 당신이 제안한 것을 시도했지만, 그것은 여전히 ​​발생합니다. 이미지를 설정하는 데 사용하는 코드와 관련이 있다고 생각합니까? 인터페이스 빌더의 뷰에 이미지를 배치하고이를 .h 파일의 IBOutlet UIImageView로 설정하면 완벽하게 돌아 다니게됩니다. 내 이미지를 설정할 때 뭔가 빠졌습니까 ?? (ps) tmpImage 팁에 대한 감사 - 그 중 하나를 놓쳤습니다! –

+0

나는 그것이 태그 문제일지도 모른다라고 생각했다. 그러나 지금 그것이보기와 함께 문제일지도 모른다라고 생각하고있다. 첫 번째 코드 비트를 사용하여 이미지를 배치하면 0,0이됩니다. 나는 그것을 끌 때 항상 거기로 돌아가고 싶다. 그래서 깜박임이 발생합니다. 현재 위치, 그곳으로 되돌아 간다 .... 나를 미치게 만들었다! –

관련 문제