jdruid
jdruid

Reputation: 613

UIView touch issue / question

Having a little trouble with my app. I am questioning my approach.

Here is an image Sample App

Basically i need the color wheel to spin in the transparent box. What I have so far is working. I can drag the color wheel and it will rotate. The problem is I can touch and drag 'anywhere' on the screen and it will rotate. I only want it to rotate in the 'window'.

I basically added a UIView and an UIImageView, added outlets to the ImageView and added code in touchesBegan and touchesMoved to perform the animation. The wheel is a full circle image and subviews are 'clipped' to no show the lower half of the image.

- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
    UITouch *thisTouch = [touches anyObject];
    delta = [thisTouch locationInView:wheelImage];

    float dx = delta.x  - wheelImage.center.x;
    float dy = delta.y  - wheelImage.center.y;
    deltaAngle = atan2(dy,dx); 

    initialTransform = wheelImage.transform;   
  }

  - (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event {
      UITouch *touch = [touches anyObject];
  CGPoint pt = [touch locationInView:wheelImage];

  float dx = pt.x  - wheelImage.center.x;
  float dy = pt.y  - wheelImage.center.y;
  float ang = atan2(dy,dx);

  //do the rotation
  if (deltaAngle == 0.0) {
    deltaAngle = ang;
    initialTransform = wheelImage.transform;
  }else
  {
    float angleDif = deltaAngle - ang;
    CGAffineTransform newTrans = CGAffineTransformRotate(initialTransform, -angleDif);
    wheelImage.transform = newTrans;
    
    currentValue = [self goodDegrees:radiansToDegrees(angleDif)];
    
  }

  }

Now...My questions are the following:

  1. How can I get the touchesMoved/Began ONLY to work on the UIView or UIImageView?
  2. Should I create a Custom UIView for this wheel and then just add it to my main view controller? 2.a. If I do create a custom, will I have the same issue with the touches?

Upvotes: 0

Views: 344

Answers (3)

Jamie
Jamie

Reputation: 5112

You may want to consider using a UISwipeGestureRecognizer and adding it to only the view in which you wish to recognize the swipe.

Upvotes: 2

Marco Mustapic
Marco Mustapic

Reputation: 3859

The easy solution is to add a BOOL var that is set true when the touch started in the region you like:

- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
    UITouch *thisTouch = [touches anyObject];
    CGPoint p = [thisTouch locationInView:wheelImage.superview];
    if (CGRectContainsPoint(wheelImage.frame, p))
    {
      rotating = YES;
      // rest of your code
      ...
    }
}

and:

- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event {
  if (rotating)
  {
     // your code
  }
}

Remember to reset rotating in touchesEnded and touchesCanceled.

Upvotes: 2

Grady Player
Grady Player

Reputation: 14549

if you subclass UIImageView and put that code in it, you will only get the touches from that view, you will probably need to set the acceptsUserInteraction to YES. also change the locationInView: to self; if you are loading from a nib, be sure to set the class to your new subclass for that UIImageView.

Upvotes: 1

Related Questions