2011-11-13 74 views
3

我想在UIImageView上應用手勢時,IAM給 姿勢縮放旋轉和圖像移動的ImageView工作的行動。所以我可以看到圖像旋轉和縮放,並從一個點移動到另一個點。GestureRecognizer問題與兩個視圖UIView和UIImageView

當我縮放圖像和縮放級別最低,並在觸及UIView這是UIImageView的超類,也給予了手勢動作。如何限制只需在UIImageView上工作而不在UIView上的手勢。並試圖將其限制在UIVIew中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的圖像。所以它看起來像旋轉。我m攻擊以下代碼。嘗試一下。這個示例是針對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]; 
        } 
       } 

    }