title: iOS: Resize and Rotate UIView Concurrently
body: <p>Using a UIPanGestureRecognizer in my view controller, I'm trying to draw a view (ArrowView) at an angle based upon the touch location. I'm trying to use CGAffineTransformRotate to rotate the view based up the angle between the first touch and the current touch, but this isn't working unless the view has already been drawn by at lease 20 or more pixels. Also, when drawing, the view doesn't always line up under my finger. Is this the correct approach for this situation? If not, does anyone recommend a better way of accomplishing this? If so, what am I doing wrong?</p>
  
  <p>ViewController.m</p>
  
  <pre><code>@implementation ViewController {
      ArrowView *_selectedArrowView;
      UIColor *_selectedColor;
      CGFloat _selectedWeight;
      CGPoint _startPoint;
  }
  
  - (void)viewDidLoad {
      [super viewDidLoad];
  
      _selectedColor = [UIColor yellowColor];
      _selectedWeight = 3;
  
      UIPanGestureRecognizer *pan = [[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(panHandler:)];
      [self.view addGestureRecognizer:pan];
  }
  
  - (void) panHandler: (UIPanGestureRecognizer *) sender {
      if (sender.state == UIGestureRecognizerStateBegan) {
  
          //Instantiate the arrow
          CGPoint touchPoint = [sender locationInView:sender.view];
          _startPoint = touchPoint;
          _selectedArrowView = [[ArrowView alloc] initWithFrame:CGRectMake(touchPoint.x, touchPoint.y, 0, 25) withColor:_selectedColor withWeight:_selectedWeight];
          _selectedArrowView.delegate = self;
          [self.view addSubview:_selectedArrowView];
          [self.view bringSubviewToFront:_selectedArrowView];
  
      } else if (sender.state == UIGestureRecognizerStateChanged) {
  
          //"Draw" the arrow based upon finger postion
          CGPoint touchPoint = [sender locationInView:sender.view];
          [_selectedArrowView drawArrow:_startPoint to:touchPoint];
      }
  }
  
  @end
  </code></pre>
  
  <p>ArrowView.m</p>
  
  <pre><code>- (void) drawArrow: (CGPoint) startPoint to: (CGPoint) endPoint {
      startPoint = [self convertPoint:startPoint fromView:self.superview];
      endPoint = [self convertPoint:endPoint fromView:self.superview];
  
      if (_initialAngle == -1000 /*Initially set to an arbitrary value so I know when the draw began*/) {
          _initialAngle = atan2(startPoint.y - endPoint.y, startPoint.x - endPoint.x);
          [self setPosition:0];
      } else {
          CGFloat ang = atan2(startPoint.y - endPoint.y, startPoint.x - endPoint.x);
          ang -= _initialAngle;
          self.transform = CGAffineTransformRotate(self.transform, ang);
  
          CGFloat diff = (endPoint.x - self.bounds.size.width);
  
          NSLog(@"\n\n diff: %f \n\n", diff);
          self.bounds = CGRectMake(0, 0, self.bounds.size.width + diff, self.bounds.size.height);
  
          _endPoint = CGPointMake(self.bounds.size.width, self.bounds.size.height);
          [self setNeedsDisplay];
      }
  }
  
  - (void) setPosition: (CGFloat) anchorPointX {
      CGPoint layerLoc;
  
      if (anchorPointX == 0) {
          layerLoc = CGPointMake(self.layer.bounds.origin.x, self.layer.bounds.origin.y + (self.layer.bounds.size.height / 2));
      } else {
          layerLoc = CGPointMake(self.layer.bounds.origin.x + self.layer.bounds.size.width, self.layer.bounds.origin.y + (self.layer.bounds.size.height / 2));
      }
  
      CGPoint superLoc = [self convertPoint:layerLoc toView:self.superview];
  
      self.layer.anchorPoint = CGPointMake(anchorPointX, 0.5);
      self.layer.position = superLoc;
  }
  
  - (CGFloat) pToA: (CGPoint) touchPoint {
      CGPoint start;
      if (_dotButtonIndex == kDotButtonFirst) {
          start = CGPointMake(CGRectGetMaxX(self.bounds), CGRectGetMaxY(self.bounds));
      } else {
          start = CGPointMake(CGRectGetMinX(self.bounds), CGRectGetMinY(self.bounds));
      }
      return atan2(start.y - touchPoint.y, start.x - touchPoint.x);
  }
  </code></pre>
  
  <p>Link to project on GitHub: <a href="https://github.com/SolaricSoftware/RotationTest" rel="nofollow">Project Link</a></p>