在可可触摸中实现基于触摸的旋转

在可可触摸中实现基于触摸的旋转

本文介绍了在可可触摸中实现基于触摸的旋转的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想知道什么是在我的iPhone应用程序中实现基于旋转的拖动运动的最佳方式。



我有一个UIView, ,当用户的手指触摸视图并且它们移动它。



基本问题归结为:



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

UITouch * touch = [touches anyObject];
CGPoint currentPoint = [touch locationInView:self]; //当前触摸位置

if(([touch view] == self)
&& [Utility getDistance:currentPoint toPoint:self.middle]< = ROTATE_RADIUS //中间是视图中心
&&& [Utility getDistance:currentPoint toPoint:self.middle]> = MOVE_RADIUS){//将是旋转手势

//记住视图状态开始触摸
CGPoint top = CGPointMake(self.middle.x,0);
self.initialTouch = currentPoint;
self.initialAngle = angleBetweenLines(self.middle,top,self.middle,currentPoint);
self.initialTransform = self.transform;
}
}

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

UITouch * touch =触摸anyObject];
CGPoint currentPoint = [touch locationInView:self]; //当前触摸位置

if(([touch view] == self)
&& amp;&<<< getDistance:currentPoint toPoint:self.middle]< = ROTATE_RADIUS
&&& amp;& amp;& amp;&&< getDistance:currentPoint toPoint:self.middle]> = MOVE_RADIUS){//旋转手势

//旋转tile
float newAngle = angleBetweenLines .middle,CGPointMake(self.middle.x,0),self.middle,currentPoint); // touch angle
float angleDif = newAngle - self.initialAngle; //在触摸开始和现在的角度之间做出差异。
CGAffineTransform newTransform = CGAffineTransformRotate(self.initialTransform,angleDif); // create new transform
self.transform = newTransform; // apply transform。
}



2)我应该记住上次已知的位置/角度,并根据它和现在之间的角度差来旋转视图,例如:

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

UITouch * touch = [touches anyObject];
CGPoint currentPoint = [touch locationInView:self]; //当前触摸位置

if(([touch view] == self)
&& amp;&<<< getDistance:currentPoint toPoint:self.middle]< = ROTATE_RADIUS
&&&& amp;& amp;& amp;& amp;&<< getDistance:currentPoint toPoint:self.middle]> = MOVE_RADIUS){//将是旋转姿态

//记住触摸开始时的视图状态
CGPoint top = CGPointMake(self.middle.x,0);
self.lastTouch = currentPoint;
self.lastAngle = angleBetweenLines(self.middle,top,self.middle,currentPoint);
}
}

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

UITouch * touch =触摸anyObject];
CGPoint currentPoint = [touch locationInView:self]; //当前触摸位置

if(([touch view] == self)
&& amp;&<< getDistance:currentPoint toPoint:middle]< = ROTATE_RADIUS
&&&&&&&& amp;&&&<<< getDistance:currentPoint toPoint:middle]> = MOVE_RADIUS){//旋转手势

//旋转平铺
float newAngle = angleBetweenLines(self.middle,CGPointMake (self.middle.x,0),self.middle,currentPoint); // touch angle
float angleDif = newAngle - self.lastAngle; //在触摸开始和现在的角度之间做出差异。
CGAffineTransform newTransform = CGAffineTransformRotate(self.transform,angleDif); // create new transform

self.transform = newTransform; // apply transform。
self.lastTouch = currentPoint;
self.lastAngle = newAngle;
}

第二个选项对我更​​有意义,结果(锯齿更新和非平滑旋转)。


解决方案

实际上比你尝试的要简单得多。



你需要三个数据点:




  • 视图的原点。

  • 当前触摸的位置




传送给您的Touch物件实际上包含最后一次触摸的位置。



所有你需要做的是计算两条线之间的角度:




  • 源于当前触摸

  • 源于以前的触摸


$ b b

然后将其转换为弧度,并在CGAffineTransformRotate()中使用它。



这里是一个函数来计算你需要什么:

  static inline CGFloat angleBetweenLinesInRadians(CGPoint line1Start,CGPoint line1End,CGPoint line2Start,CGPoint line2End){

CGFloat a = line1End.x - line1Start.x;
CGFloat b = line1End.y - line1Start.y;
CGFloat c = line2End.x - line2Start.x;
CGFloat d = line2End.y - line2Start.y;

CGFloat line1Slope =(line1End.y - line1Start.y)/(line1End.x - line1Start.x);
CGFloat line2Slope =(line2End.y - line2Start.y)/(line2End.x - line2Start.x);

CGFloat degs = acosf((a * c)+(b * d))/((sqrt(a * a + b * b))* d))));


return(line2Slope> line1Slope)? degs:-degs;
}

由Jeff LaMarche提供:





示例:

  UITouch * touch = [touches anyObject]; 
CGPoint origin = [view center];
CGFloat angle = angleBetweenLinesInRadians(origin,[touch previousLocationInView:self.superview.superview],origin,[touch locationInView:self.superview.superview]);


I am wondering what is the best way to implement rotation-based dragging movements in my iPhone application.

I have a UIView that I wish to rotate around its centre, when the users finger is touch the view and they move it. Think of it like a dial that needs to be adjusted with the finger.

The basic question comes down to:

1) Should I remember the initial angle and transform when touchesBegan is called, and then every time touchesMoved is called apply a new transform to the view based on the current position of the finger, e.g., something like:

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

  UITouch *touch = [touches anyObject];
  CGPoint currentPoint = [touch locationInView:self]; //current position of touch

 if (([touch view] == self)
    &&  [Utility getDistance:currentPoint toPoint:self.middle] <= ROTATE_RADIUS //middle is centre of view
    && [Utility getDistance:currentPoint toPoint:self.middle] >= MOVE_RADIUS) { //will be rotation gesture

  //remember state of view at beginning of touch
  CGPoint top = CGPointMake(self.middle.x, 0);
  self.initialTouch = currentPoint;
  self.initialAngle = angleBetweenLines(self.middle, top, self.middle, currentPoint);
  self.initialTransform = self.transform;
 }
}

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

  UITouch *touch = [touches anyObject];
  CGPoint currentPoint = [touch locationInView:self]; //current position of touch

  if (([touch view] == self)
  &&  [Utility getDistance:currentPoint toPoint:self.middle] <= ROTATE_RADIUS
  && [Utility getDistance:currentPoint toPoint:self.middle] >= MOVE_RADIUS) { //a rotation gesture

  //rotate tile
  float newAngle = angleBetweenLines(self.middle, CGPointMake(self.middle.x, 0), self.middle, currentPoint); //touch angle
  float angleDif = newAngle - self.initialAngle; //work out dif between angle at beginning of touch and now.
  CGAffineTransform newTransform = CGAffineTransformRotate(self.initialTransform, angleDif); //create new transform
  self.transform = newTransform;  //apply transform.
}

OR

2) Should I simply remember the last known position/angle, and rotate the view based on the difference in angle between that and now, e.g.,:

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

      UITouch *touch = [touches anyObject];
      CGPoint currentPoint = [touch locationInView:self]; //current position of touch

     if (([touch view] == self)
        &&  [Utility getDistance:currentPoint toPoint:self.middle] <= ROTATE_RADIUS
        && [Utility getDistance:currentPoint toPoint:self.middle] >= MOVE_RADIUS) { //will be rotation gesture

      //remember state of view at beginning of touch
      CGPoint top = CGPointMake(self.middle.x, 0);
      self.lastTouch = currentPoint;
      self.lastAngle = angleBetweenLines(self.middle, top, self.middle, currentPoint);
     }
    }

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

      UITouch *touch = [touches anyObject];
      CGPoint currentPoint = [touch locationInView:self]; //current position of touch

      if (([touch view] == self)
      &&  [Utility getDistance:currentPoint toPoint:middle] <= ROTATE_RADIUS
      && [Utility getDistance:currentPoint toPoint:middle] >= MOVE_RADIUS) { //a rotation gesture

      //rotate tile
      float newAngle = angleBetweenLines(self.middle, CGPointMake(self.middle.x, 0), self.middle, currentPoint); //touch angle
      float angleDif = newAngle - self.lastAngle; //work out dif between angle at beginning of touch and now.
      CGAffineTransform newTransform = CGAffineTransformRotate(self.transform, angleDif); //create new transform

      self.transform = newTransform;  //apply transform.
      self.lastTouch = currentPoint;
      self.lastAngle = newAngle;
    }

The second option makes more sense to me, but it is not giving very pleasing results (jaggy updates and non-smooth rotations). Which way is best (if any), in terms of performance?

Cheers!

解决方案

It is actually much simpler than what you have tried.

You need three data points:

  • The origin of your view.
  • The location of the current touch
  • The location of the previous touch

The Touch object passed to you actually contains the last touch location. So you don't need to keep track of it.

All you have to do is calculate the angle between two lines:

  • Origin to Current Touch
  • Origin to Previous Touch

Then convert that to radians and use that in your CGAffineTransformRotate(). Do that all in your touchesMoved handler.

Here is a function to calculate what you need just that:

static inline CGFloat angleBetweenLinesInRadians(CGPoint line1Start, CGPoint line1End, CGPoint line2Start, CGPoint line2End) {

    CGFloat a = line1End.x - line1Start.x;
    CGFloat b = line1End.y - line1Start.y;
    CGFloat c = line2End.x - line2Start.x;
    CGFloat d = line2End.y - line2Start.y;

    CGFloat line1Slope = (line1End.y - line1Start.y) / (line1End.x - line1Start.x);
    CGFloat line2Slope = (line2End.y - line2Start.y) / (line2End.x - line2Start.x);

    CGFloat degs = acosf(((a*c) + (b*d)) / ((sqrt(a*a + b*b)) * (sqrt(c*c + d*d))));


    return (line2Slope > line1Slope) ? degs : -degs;
}

Courtesy of Jeff LaMarche at:

http://iphonedevelopment.blogspot.com/2009/12/better-two-finger-rotate-gesture.html

Example:

UITouch *touch = [touches anyObject];
CGPoint origin = [view center];
CGFloat angle = angleBetweenLinesInRadians(origin, [touch previousLocationInView:self.superview.superview], origin, [touch locationInView:self.superview.superview]);

这篇关于在可可触摸中实现基于触摸的旋转的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-20 12:49