手势识别器在相同的图片处捏合和平移 - 松开中心以进行捏合

时间:2014-05-26 10:38:02

标签: ios uigesturerecognizer uipangesturerecognizer uipinchgesturerecognizer

我的问题是我无法使用Pinch和Pan( 这两个功能完全分开。但如果我移动pic - 缩放有错误的中心锚(两个手指之间) 代码:

// fullScreenImage是我想操作的UIImageView

UIPinchGestureRecognizer *pinchRecognizer = [[UIPinchGestureRecognizer alloc] initWithTarget:self action:@selector(scalePiece:)];
pinchRecognizer setDelegate:(id)self];
[fullScreenImage.superview addGestureRecognizer:pinchRecognizer];

UIPanGestureRecognizer *panRecognizer = [[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(move:)];
[panRecognizer setMinimumNumberOfTouches:1];
[panRecognizer setMaximumNumberOfTouches:1];
[panRecognizer setDelegate:(id)self];
[self.view addGestureRecognizer:panRecognizer];




- (void)scalePiece:(UIPinchGestureRecognizer *)gestureRecognizer
{
  [self adjustAnchorPointForGestureRecognizer:gestureRecognizer]; 
    if ([gestureRecognizer state] == UIGestureRecognizerStateBegan || [gestureRecognizer state] == UIGestureRecognizerStateChanged) {
        [gestureRecognizer view].transform = CGAffineTransformScale([[gestureRecognizer view] transform], [gestureRecognizer scale], [gestureRecognizer scale]);
        [gestureRecognizer setScale:1];
}

//在两根手指之间设置锚点以正确缩放

- (void)adjustAnchorPointForGestureRecognizer:(UIGestureRecognizer *)gestureRecognizer {
   if (gestureRecognizer.state == UIGestureRecognizerStateBegan) {
       UIView *piece = gestureRecognizer.view;
        CGPoint locationInView = CGPointMake([gestureRecognizer locationInView:piece].x,[gestureRecognizer locationInView:piece].y);
        CGPoint locationInSuperview = CGPointMake([gestureRecognizer locationInView:piece.superview].x,[gestureRecognizer locationInView:piece.superview].y);
        piece.layer.anchorPoint = CGPointMake(locationInView.x / piece.bounds.size.width, locationInView.y / piece.bounds.size.height);
        piece.center = locationInSuperview;

    }
}


- (void)move:(UIPinchGestureRecognizer *)recognizer
{
    if ([(UIPanGestureRecognizer*)recognizer state] == UIGestureRecognizerStateBegan) {
        position.y=[recognizer locationInView:recognizer.view].y-fullScreenImage.center.y;
        position.x=[recognizer locationInView:recognizer.view].x-fullScreenImage.center.x;
    }
  fullScreenImage.center=CGPointMake([recognizer locationInView:fullScreenImage.superview].x-position.x, [recognizer locationInView:fullScreenImage.superview].y-position.y);
} 

2 个答案:

答案 0 :(得分:0)

试试这个:

在您的代码中添加此方法:

  - (void)adjustAnchorPointForGestureRecognizer:(UIGestureRecognizer *)gestureRecognizer
  {
    UIView *piece = gestureRecognizer.view;
    CGPoint locationInView = [gestureRecognizer locationInView:piece];
    CGPoint locationInSuperview = [gestureRecognizer locationInView:piece.superview];

    piece.layer.anchorPoint = CGPointMake(locationInView.x / piece.bounds.size.width, locationInView.y / piece.bounds.size.height);
    piece.center = locationInSuperview;
  }

  - (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldRecognizeSimultaneouslyWithGestureRecognizer:(UIGestureRecognizer *)otherGestureRecognizer
  {
     return YES;
  }

使用手势时调用此方法。

 - (void)scalePiece_image:(UIPinchGestureRecognizer *)gestureRecognizer
 {
     [self adjustAnchorPointForGestureRecognizer:gestureRecognizer];

     if ([gestureRecognizer state] == UIGestureRecognizerStateBegan || [gestureRecognizer state] == UIGestureRecognizerStateChanged)
     {
         [gestureRecognizer view].transform = CGAffineTransformScale([[gestureRecognizer view] transform], [gestureRecognizer scale], [gestureRecognizer scale]);
         [gestureRecognizer setScale:1];
     }
 }

答案 1 :(得分:0)

SR Nayak的回答不正确,因为当您捏合时图层会发生变化,图层会滑动。

此代码有效:

$tasks = Team::currentTeam()
    ->tasks()
    ->assignedToUser()
    ->where(function ($query) use ($start_date, $end_date) {
        $query->where([
            // Task falls on or between date range
            ['start_date', '>=', $start_date],
            ['start_date', '<=', $end_date],
            ['due_date', '>=', $start_date],
            ['due_date', '<=', $end_date]
        ])                
        ->orWhere([
            // Task is overdue from previous week
            ['start_date', '<', $start_date],
            ['due_date', '<', $start_date],
            ['status', '!=', 'completed']
        ])                
        ->orWhere([
            // Task start before date range
            // and due date is past end of date range
            ['start_date', '<', $start_date],
            ['due_date', '>', $end_date]
        ])
        ->orWhere([
            // Task start within date range, 
            // but due date is past end of date range
            ['start_date', '>=', $start_date],
            ['start_date', '<=', $end_date],
            ['due_date', '>', $end_date]
        ])
        ->orWhere([
            // Task due date within date range, 
            // but start date is before start of date range
            ['due_date', '>=', $start_date],
            ['due_date', '<=', $end_date],
            ['start_date', '<', $start_date]
        ]);
    })            
->get();