2013-11-21 108 views
0

如何編寫自定義手勢以在所有方向(即)視圖的原始邊和大小上增加視圖的大小。用於增加視圖大小的自定義手勢

實施例:

  1. 用戶應該觸摸在角落中的任何一個的圖。無論是靠近原點還是觀看端。

示例:查看框架尺寸爲(100,100,200,200).,最小觸摸距離爲任意一側的15個像素。

所以,如果用戶觸摸位置是在(X=15,Y=40)

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

方法意味着觸摸靠近原點側。

相同的,如果所述用戶觸摸位置是(X=15, Y=190) in

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

方法意味着觸摸是附近的大小側。

我的代碼:

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

{ 

[super touchesBegan:touches withEvent:event]; 
if([touches count] != self.touchMinimumCount || 
    [[touches anyObject] tapCount] > self.tapMinimumCount) 
{ 
    self.state = UIGestureRecognizerStateFailed; 
    return; 
} 
self.startPoint = [[touches anyObject] locationInView:self.view]; 
self.viewFrame = self.view.frame; 

if(((self.startPoint.x - self.view.bounds.origin.x) <= self.minimumTouchDistance) || 
    ((self.startPoint.y - self.view.bounds.origin.y) <= self.minimumTouchDistance) || 
    ((ABS(self.startPoint.x - self.view.bounds.size.width)) <= self.minimumTouchDistance) || 
    ((ABS(self.startPoint.y - self.view.bounds.size.height)) <= self.minimumTouchDistance)) 
{ 
    self.state = UIGestureRecognizerStatePossible; 
    if(((self.startPoint.x - self.view.bounds.origin.x) <= self.minimumTouchDistance) || 
     ((self.startPoint.y - self.view.bounds.origin.y) <= self.minimumTouchDistance)) 
    { 
     self.currentTouchPosition = touchPositionIsOrigin; 
    } 
    else if(((ABS(self.startPoint.x - self.view.bounds.size.width)) <= self.minimumTouchDistance) || 
      ((ABS(self.startPoint.y - self.view.bounds.size.height)) <= self.minimumTouchDistance)) 
    { 
     self.currentTouchPosition = touchPositionIsSize; 
    } 
} 
else 
{ 
    self.state = UIGestureRecognizerStateFailed; 
} 
} 

我的手勢可能狀態如果僅觸摸距離在側觸摸設置。

現在我想調整視圖框架根據用戶手指移動。

  1. 如果用戶觸摸原點並向左方向移動意味着要更改視圖的origin.x並且寬度也要增加。
  2. 如果用戶觸摸原點並向頂部方向移動意味着要更改視圖的原點並且寬度也要增加。
  3. 如果用戶觸摸原點並向右方向移動意味着要更改視圖的origin.x。
  4. 如果用戶觸摸原點並向底部方向移動意味着要更改視圖的原點。
  5. 如果用戶觸摸大小的一面並向左方向移動意味着要減小視圖的寬度。
  6. 如果用戶觸摸大小的一面並向頂部方向移動意味着要降低視圖的高度。
  7. 如果用戶觸摸大小的一面並朝着正確的方向移動意味着要增加視圖的寬度。
  8. 如果用戶觸摸大小的一面朝底部方向移動意味着要增加視圖的高度。

這一切都根據移動中的用戶手指在該視圖中改變。

我的代碼:

- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event 

{ 

[super touchesMoved:touches withEvent:event]; 
if(self.state == UIGestureRecognizerStateFailed) return; 
direction currentDirection; 
CGPoint currentLocation = [[touches anyObject] locationInView:self.view]; 
if(self.state == UIGestureRecognizerStatePossible || 
    self.state == UIGestureRecognizerStateChanged) 
{ 
    if((currentLocation.x - self.startPoint.x) > 0) 
    { 
     currentDirection = directionRight; 
     self.state = UIGestureRecognizerStateChanged; 
     if(self.currentTouchPosition == touchPositionIsOrigin) 
     { 
      distanceInOriginX = // need to implement. 
      distanceInSizeX = // need to implement. 
     } 
     else if(self.currentTouchPosition == touchPositionIsSize) 
     { 
      distanceInSizeX = // need to implement. 
     } 
    } 
    else if((currentLocation.x - self.startPoint.x) < 0) 
    { 
     currentDirection = directionLeft; 
     self.state = UIGestureRecognizerStateChanged; 
     if(self.currentTouchPosition == touchPositionIsOrigin) 
     { 
      distanceInOriginX = // need to implement. 
      distanceInSizeX = // need to implement. 
     } 
     else if(self.currentTouchPosition == touchPositionIsSize) 
     { 
      distanceInSizeX = // need to implement. 
     } 
    } 

    else if((currentLocation.y - self.startPoint.y) >= 0) 
    { 
     currentDirection = directionBottom; 
     self.state = UIGestureRecognizerStateChanged; 
     if(self.currentTouchPosition == touchPositionIsOrigin) 
     { 
      distanceInOriginY = // need to implement. 
      distanceInSizeY = // need to implement. 
     } 
     else if(self.currentTouchPosition == touchPositionIsSize) 
     { 
      distanceInSizeY = // need to implement. 
     } 
    } 
    else if((currentLocation.y - self.startPoint.y) < 0) 
    { 
     currentDirection = directionTop; 
     self.state = UIGestureRecognizerStateChanged; 
     if(self.currentTouchPosition == touchPositionIsOrigin) 
     { 
      distanceInOriginY = // need to implement. 
      distanceInSizeY = // need to implement. 
     } 
     else if(self.currentTouchPosition == touchPositionIsSize) 
     { 
      distanceInSizeY = // need to implement. 
     } 
    } 
    else 
    { 
     currentDirection = directionUnknown; 
    } 
    self.originPoint = CGPointMake(distanceInOriginX, distanceInOriginY); 
    self.sizePoint = CGPointMake(distanceInSizeX, distanceInSizeY); 
} 
} 

我提到//需要執行。哪裏我想要的代碼來實現我提到的功能。所以,請大家幫忙。

如果我是錯的意思,分享最好的方式來做同樣類型的手勢。

任何示例在這裏也很好地分享。在此先感謝....

回答

0

如果要縮放視圖的大小,然後使用UIPinchGestureRecognizer。示例代碼這裏給出

UIPinchGestureRecognizer *pinchRecognizer = [[UIPinchGestureRecognizer alloc]initWithTarget:self action:@selector(scale:)]; 
    pinchRecognizer.delegate = self; 
    [self addGestureRecognizer:pinchRecognizer]; 

處理方法是

#pragma mark - Gesture handling functions 



-(void)scale:(UIPinchGestureRecognizer*)recognizer { 

     if ([recognizer state] == UIGestureRecognizerStateBegan) { 
      _lastScale = 1.0; 
     } 

     CGFloat scale = 1.0 - (_lastScale - [recognizer scale]); 

     CGAffineTransform currentTransform = self.imgView.transform; 
     CGAffineTransform newTransform = CGAffineTransformScale(currentTransform, scale, scale); 

     self.imgView.transform = newTransform; 

     _lastScale = [recognizer scale]; 

    } 
+0

但捏合手勢將調整視圖中的所有方面,但我只在一次想一邊。 – Nagarajan