2011-11-13 2 views
3

iam이 제스처 동작을 이미지 뷰 작업에서 이미지의 회전 및 이동 비율을 조정할 때 UIImageView에 제스처를 적용하려고합니다. 그래서 나는 이미지를 회전하고 크기를 조정하고 한 지점에서 다른 지점으로 이동하는 것을 볼 수 있습니다.두 개의보기 UIView 및 UIImageView가있는 GestureRecognizer 문제

이미지 및 확대/축소 수준을 최소로 조정하고 제스처 동작을 제공하는 UIImageView의 수퍼 클래스 인 UIView를 터치 할 때. UIView가 아닌 ​​UIImageView에서만 작동해야하는 제스처를 제한하는 방법. Imageview가 뷰 외부에 있어야 할 필요는 없습니다.

-(void)InitGestures{ 



    tapRecognizer = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(handleSingleTap:)]; 
    tapRecognizer.numberOfTouchesRequired = 1; 
    templatePhotoPlaceholderView=[[UIView alloc]init]; 
    templatePhotoPlaceholderView.clipsToBounds = YES; 


    //templatephotoplaceholder frame setting 
    [self templatePhotoPlaceholderFrameSetting]; 

    templatePhotoPlaceholderView.backgroundColor=[UIColor colorWithRed:0.8823 green:0.8823 blue:0.8823 alpha:1]; 
    [self photoView:templatePhotoPlaceholderView]; 

    tapRecognizer.view.frame=templatePhotoPlaceholderView.frame; 
    [self photoButtonPlaceHolder:templatePhotoPlaceholderView]; 

    [selectedTemplateImage addSubview:templatePhotoPlaceholderView]; 
    [templatePhotoPlaceholderView addGestureRecognizer:tapRecognizer]; 



    UIPanGestureRecognizer *panRecognizer = [[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(move:)]; 
    [panRecognizer setMinimumNumberOfTouches:1]; 

    [panRecognizer setMaximumNumberOfTouches:1]; 
    [panRecognizer setDelegate:self]; 
    [templatePhotoPlaceholderView addGestureRecognizer:panRecognizer]; 
    //[panRecognizer release]; 


    UIPinchGestureRecognizer *pinchRecognizer = [[[UIPinchGestureRecognizer alloc] initWithTarget:self action:@selector(scale:)] autorelease]; 
    [pinchRecognizer setDelegate:self]; 
    [self.view addGestureRecognizer:pinchRecognizer]; 
    //[pinchRecognizer release]; 

    [self.view addSubview:templatePhotoPlaceholderView]; 
    [tapRecognizer release]; 

} 


-(void)move:(UIPanGestureRecognizer *)gestureRecognizer 
{ 
    CGPoint translatedPoint = [gestureRecognizer translationInView:templatePhotoPlaceholderView]; 

    if([gestureRecognizer state] == UIGestureRecognizerStateBegan) { 
     _firstX = [imageview center].x; 
     _firstY = [imageview center].y; 
    } 



    translatedPoint = CGPointMake(_firstX+translatedPoint.x, _firstY+translatedPoint.y); 

    [imageview setCenter:translatedPoint]; 

} 
+0

특정 경계에서 동영상을 제한하는 방법을 알고 계실 것입니다. – user905582

+0

슈퍼 뷰의 프레임을 제한하면됩니다. – kiran

답변

0

다음 코드는 수퍼 경계

if (recognizer.state == UIGestureRecognizerStateBegan) 
{ 
    previousPoint = [recognizer translationInView:self]; 
} 
else if (recognizer.state == UIGestureRecognizerStateChanged) 
{ 

    CGRect parentFrame = self.frame; 
    CGRect currentFrame = imageView.frame; 

    if (CGRectContainsRect(parentFrame, currentFrame)) 
    { 
     CGPoint point = [recognizer translationInView:self.superview]; 
     CGPoint movePoint = CGPointMake(point.x - previousPoint.x, point.y - previousPoint.y); 
     CGPoint newCenter = CGPointMake(imageView.center.x + movePoint.x, imageView.center.y + movePoint.y); 
     imageView.center = newCenter; 
     previousPoint = point; 
    } 
    else 
    { 
     CGRect imageRect = imageView.frame; 
     if (imageRect.origin.x < 0) 
      imageRect.origin.x = 0; 
     else if (imageRect.origin.x + imageRect.size.width >= self.frame.size.width) 
      imageRect.origin.x = self.frame.size.width - imageRect.size.width; 

     if (imageRect.origin.y < 0) 
      imageRect.origin.y = 0; 
     else if (imageRect.origin.y + imageRect.size.height >= self.frame.size.height) 
      imageRect.origin.y = self.frame.size.height - imageRect.size.height; 

     imageView.frame = imageRect; 
    } 
} 
1

먼저 당신의 UIView에서 UIImageView에 추가 내에서 움직이는 이미지를 제한합니다. 각 UIView에 제스처를 적용합니다. UIImageView의 이미지를 변경하십시오. 그래서 그것은 회전처럼 보입니다. 다음 코드를 공격합니다. 그것을 시도하십시오. 이 샘플은 가로로 3 UIView입니다.

-(void)addSwipeEvent:(UIView*)subView{ 

     UISwipeGestureRecognizer *recognizer = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(SwipeRecognizer:)]; 
     recognizer.numberOfTouchesRequired = 1; 
     recognizer.delegate = self; 
     [subView addGestureRecognizer:recognizer]; 
     [recognizer release]; 

     UISwipeGestureRecognizer *leftRecognizer = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(SwipeRecognizer:)]; 
     leftRecognizer.direction=UISwipeGestureRecognizerDirectionLeft; 
     leftRecognizer.numberOfTouchesRequired = 1; 
     recognizer.delegate = self; 
     [subView addGestureRecognizer:leftRecognizer]; 
     [leftRecognizer release]; 

     UISwipeGestureRecognizer *downRecognizer = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(SwipeRecognizer:)]; 
     downRecognizer.direction=UISwipeGestureRecognizerDirectionDown; 
     downRecognizer.numberOfTouchesRequired = 1; 
     recognizer.delegate = self; 
     [subView addGestureRecognizer:downRecognizer]; 
     [downRecognizer release]; 

     UISwipeGestureRecognizer *upRecognizer = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(SwipeRecognizer:)]; 
     upRecognizer.direction=UISwipeGestureRecognizerDirectionUp; 
     upRecognizer.numberOfTouchesRequired = 1; 
     recognizer.delegate = self; 
     [subView addGestureRecognizer:upRecognizer]; 
     [upRecognizer release]; 
    } 

    - (void) SwipeRecognizer:(UISwipeGestureRecognizer *)sender { 
     if (sender.direction == UISwipeGestureRecognizerDirectionLeft){ 
      NSLog(@" *** SWIPE LEFT ***"); 

     } 
     if (sender.direction == UISwipeGestureRecognizerDirectionRight){ 
      NSLog(@" *** SWIPE RIGHT ***"); 

     } 
     if (sender.direction== UISwipeGestureRecognizerDirectionUp){ 
      NSLog(@" *** SWIPE UP ***"); 

     } 
     if (sender.direction == UISwipeGestureRecognizerDirectionDown){ 
      NSLog(@" *** SWIPE DOWN ***"); 

     } 
    } 

    - (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldReceiveTouch:(UITouch *)touch { 
     if ([touch.view isKindOfClass:[UIView class]]) 
     { 
      viewIndex = touch.view.tag; 
      return YES; 
     } 
     return NO; 
    } 

    -(void)lowLevelSwipeRecognizer{ 

      CATransition *myTransition = [CATransition animation]; 
      myTransition.type = kCATransitionPush; 
      myTransition.duration = 0.3; 
      myTransition.subtype = kCATransitionFromRight; 

       NSString *str = [arrayImg objectAtIndex:0]; 

       for (int i=0; i<2; i++) { 
        [arrayImg replaceObjectAtIndex:i withObject:[arrayImg objectAtIndex:i+1]]; 

       } 
       [arrayImg replaceObjectAtIndex:2 withObject:str]; 


       for (int cnt = 0; cnt<3; cnt++) { 
        if ([[arrayView objectAtIndex:cnt] isKindOfClass:[CustomView class]]) { 
         CustomView *v = (CustomView *)[arrayView objectAtIndex:cnt]; 
         [v.imageView setImage:[UIImage imageNamed:[arrayImg objectAtIndex:cnt]]]; 
         [v.layer addAnimation:myTransition forKey:nil]; 
        } 
       } 

    } 
관련 문제