Question

I have a custom UIView with some buttons as subviews. I am using touchesBegan/Moved/Ended to rotate the view with the user's finger. The problem is, when I try to drag/rotate the view from a button, the touches methods aren't called. I need the buttons to respond to the UIControlEventTouchUpInside event, but if it is dragged with the view, then to make my view rotate. I have looked at this question and this question, but couldn't get either solution to work for me.

Here is my custom view code:

- (id)initWithFrame:(CGRect)frame andRadius:(float)Radius andViews:

(NSMutableArray *)AllViews
{
    self = [super initWithFrame:frame];
    if (self) {
        radius = Radius;
        allViews = AllViews;

        for(int i = 0; i < [allViews count]; i++){
            //the "currentView" is a UIButton
            UIView *currentView = (UIView *)[allViews objectAtIndex:i];
            [currentView setFrame:CGRectMake(frame.size.width / 2 - 25 + radius * cos((2 * M_PI / [allViews count]) * i), frame.size.height / 2 - 25 + radius * sin((2 * M_PI / [allViews count]) * i), 50, 50)];
            [self addSubview:currentView];

        }
        [self setBackgroundColor:[UIColor redColor]];
        [self setAlpha:.5];

    }
    return self;
}

//- (BOOL)pointInside:(CGPoint)point withEvent:(UIEvent *)event
//{
//    for (UIView * view in [self subviews]) {
//        if ([view pointInside:[self convertPoint:point toView:view] withEvent:event]) {
//            return YES;
//        }
//    }
//    return NO;
//}

- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event 
{
    UITouch *touch = [touches anyObject];
    CGPoint point = [touch locationInView:self];

    x = point.x;
    y = point.y;

    NSLog(@"touchesBegan");
} 

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

    float X = point.x;
    float Y = point.y;

    float a = sqrt(X * X + Y * Y);
    float b = sqrt(x * x + y * y);
    float c = sqrt((x - X) * (x - X) + (y - Y) * (y - Y));

    float alpha = acos((a * a + b * b - (c * c)) / (2 * a * b));
    if(alpha == NAN)
        alpha = 0;

    alpha = -alpha;


    [UIView beginAnimations:@"rotate" context:nil];
    [UIView setAnimationDuration:0];
    [UIView setAnimationCurve:UIViewAnimationCurveEaseOut];
    self.transform = CGAffineTransformRotate(self.transform, alpha);
    [UIView commitAnimations];
}
- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event 
{
    UITouch *touch = [touches anyObject];
    CGPoint point = [touch locationInView:self];

    float X = point.x;
    float Y = point.y;

    float a = sqrt(X * X + Y * Y);
    float b = sqrt(x * x + y * y);
    float c = sqrt((x - X) * (x - X) + (y - Y) * (y - Y));

    float alpha = acos((a * a + b * b - (c * c)) / (2 * a * b));

    alpha = -alpha;


    [UIView beginAnimations:@"rotate" context:nil];
    [UIView setAnimationDuration:0.5];
    [UIView setAnimationCurve:UIViewAnimationCurveEaseOut];
    self.transform = CGAffineTransformRotate(self.transform, alpha);
    [UIView commitAnimations];
    NSLog(@"touchesEnded");
}

- (void)dealloc
{
    [allViews release];
    [super dealloc];
}

Any suggestions?

Was it helpful?

Solution 2

Just incase someone comes across a similar issue, I'll post the solution I used. I overrode another UIView class that I used as a button. In each of the touchedBegan/Moved/Ended, I implemented the code I need for the button click, then passed the touch up the chain of command to the next custom UIView, like so:

- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
    [self.nextResponder touchesBegan:touches withEvent:event];
}

- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event
{
    [self.nextResponder touchesMoved:touches withEvent:event];
}

- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event
{
    UITouch *touch = [touches anyObject];
    CGPoint point = [touch locationInView:self];

    if (point.x < self.frame.size.width && point.y < self.frame.size.height) {
        NSDictionary *userInfo = [[NSDictionary alloc] initWithObjectsAndKeys:[NSString stringWithFormat:@"%d", self.tag], @"tag", nil];
        [[NSNotificationCenter defaultCenter] postNotificationName:@"ChangeFilter" object:self userInfo:userInfo];
    }


    [self.nextResponder touchesEnded:touches withEvent:event];
}

Notice that I use NSNotifications to send the action to my ViewController that contains the overridden view.

OTHER TIPS

Maybe not the answer you're looking for, but:

I'm not sure why you'd want "finger down on button then drag finger outside of button" activity to be picked up by the view behind the button as a dragging/rotating motion. As far as I'm concerned, if you put your finger down inside a button, you are interacting with the button, not the view behind; and if you move your finger off the button while still touching the screen, you're doing that to abort the button press (i.e. you've "armed" it but not "triggered" it), not to interact with the view behind.

So what I'm saying is: why would you want to do this? Maybe you need to rethink your UI design.

If you absolutely want to do this, you may want to look at using UIGestureRecognizers instead of touchesBegan etc. - you can set up more complicated scenarios more easily when using them. But the gesture recognizers are not available on all versions of iOS.

Alternatively, implement something that looks like a button or whatever it is you want, and handle the touching of it (and the view dragging etc.) using touchesBegan etc. - i.e. don't use an actual button.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top