2012-02-16 2 views
3

내 코드에는 UIImages가있는 UIImageViews 인 개체 배열이 있습니다.특정 UIImageView 끌기

-(void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event의 for 루프를 사용하여 배열의 모든 객체를 하나씩 이동하십시오.

일단 주보기에서 다른 위치로 이동하면 해당 개체를 움켜 잡고 다른 곳으로 이동할 수 있기를 바랍니다.

나는 이것을 가장 잘 수행 할 방법이 확실하지 않습니다. 내가 클릭 한 항목 만 끌 수 있도록 touchesbegin을 사용할 수있는 방법이 있습니까?

기본적으로 UIImageView가 5 개 있으며 각각 UIImage가 있으며 그 중 하나를 클릭하면 이동할 수있는 지점에 도달하려고합니다.

당신은, 당신이 이동하려는 뷰 제스처 인식기를 추가하는 것을 고려하는 그들은 도청하고 당신이 그들 주위를 이동할 수있는 touchesMoved을 사용할 수있을 때 쉽게 말할 수있는 그런 식으로 할 수 있습니다

답변

1

내가했습니다 위대한 성공과 비슷한 것을했는데 UIPanGestureRecognizer을 드래그 할 수 있습니다. 그것을를 연결하기 위해서는 다음과 같이 보일 수 있습니다 당신의 pan: 방법에서

@implementation DraggableView 

-(id)initWithFrame:(CGRect)frame 
{ 
    ... 
    ... 
    UIPanGestureRecognizer *panGesture = [[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(pan:)] autorelease]; 
    [self addGestureRecognizer:panGesture]; 
    .. 
    return self; 
} 
@end 

, 당신은 제스처의 상태를 확인하고 심지어 제스처의 위치를 ​​포착하고보기를 재배치하는 데 사용할 수 있습니다. UIPanGestureRecognizer

-(void)pan:(UIPanGestureRecognizer *)gesture 
{ 

    if (gesture.state==UIGestureRecognizerStateChanged) 
    { 
     //We're moving! 
     UIView *aRootView = <perhaps your root view controller's view>; 
     CGPoint currentOrigin = [gesture translationInView:aRootView]; 
     self.frame = CGRectMake(currentOrigin.x,currentOrigin.y,self.frame.size.width,self.frame.size.hight); 
     ... 
     ... 
    } 

} 

Here is more reading : 여기가 어떻게 보이는지의 거친 예입니다.

+0

감사! 그게 내가 필요한 것입니다 –

2

편집 : John Muchow가 here 블로그에 올린 글입니다. 당신은 자신의 목적에 맞게 적응할 수 있어야합니다.

//UIDraggableImageView.h 
#import <UIKit/UIKit.h> 

@interface UIDraggableImageView : UIImageView 
{ 
    CGPoint currentPoint; 
} 

@end 

//UIDraggableImageView.m 
#import "UIDraggableImageView.h" 

@implementation UIDraggableImageView 

- (id)initWithImage:(UIImage *)image 
{ 
    if (self = [super initWithImage:image]) { 
     self.userInteractionEnabled = YES; 
    } 
    return self; 
} 

-(void)dealloc { 
    [super dealloc]; 
} 

- (void) touchesBegan:(NSSet*)touches withEvent:(UIEvent*)event 
{ 
    // When a touch starts, get the current location in the view 
    currentPoint = [[touches anyObject] locationInView:self]; 
} 

- (void) touchesMoved:(NSSet*)touches withEvent:(UIEvent*)event 
{ 
    // Get active location upon move 
    CGPoint activePoint = [[touches anyObject] locationInView:self]; 

    // Determine new point based on where the touch is now located 
    CGPoint newPoint = CGPointMake(self.center.x + (activePoint.x - currentPoint.x), 
            self.center.y + (activePoint.y - currentPoint.y)); 

    //-------------------------------------------------------- 
    // Make sure we stay within the bounds of the parent view 
    //-------------------------------------------------------- 
    float midPointX = CGRectGetMidX(self.bounds); 
    // If too far right... 
    if (newPoint.x > self.superview.bounds.size.width - midPointX) 
     newPoint.x = self.superview.bounds.size.width - midPointX; 
    else if (newPoint.x < midPointX) // If too far left... 
     newPoint.x = midPointX; 

    float midPointY = CGRectGetMidY(self.bounds); 
    // If too far down... 
    if (newPoint.y > self.superview.bounds.size.height - midPointY) 
     newPoint.y = self.superview.bounds.size.height - midPointY; 
     else if (newPoint.y < midPointY) // If too far up... 
     newPoint.y = midPointY; 

    // Set new center location 
    self.center = newPoint; 
} 

@end 
-1

버튼을 클릭하여 카메라 롤에서 이미지 뷰로 이미지를 추가합니다. 이미지를 확대하거나 축소 할 수 있으며 해당 이미지를 뷰의 원하는 위치로 드래그해야합니다.

+0

제발, 귀하의 대답을 단계별로 작성하고 코드를 추가하여 이미 답변을 시도해 보았습니다. –