需要UIButton里面的自定义UIView响应触摸

时间:2012-05-15 17:41:43

标签: iphone uiview touchesbegan touchesmoved

我有一个自定义UIView,其中一些按钮作为子视图。我使用touchesBegan / Moved / Ended用用户的手指旋转视图。问题是,当我尝试从按钮拖动/旋转视图时,不会调用touches方法。我需要按钮来响应UIControlEventTouchUpInside事件,但是如果它与视图一起拖动,那么我的视图会旋转。我查看了this问题和this问题,但无法找到适合我的解决方案。

这是我的自定义视图代码:

- (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];
}

有什么建议吗?

2 个答案:

答案 0 :(得分:0)

也许不是你想要的答案,但是:

我不确定你为什么要“按下手指按钮然后将手指拖到按钮外”活动被按钮后面的视图拾取为拖动/旋转动作。就我而言,如果你把手指放在按钮内,你就会与按钮交互,而不是背后的视图;如果你在仍然触摸屏幕的同时将手指从按钮上移开,你就是这样做就是中止按下按钮(即你已经“武装”它但没有“触发”它),而不是与后面的视图互动。

所以我说的是:你为什么要这样做?也许您需要重新考虑您的UI设计。

如果您绝对想要这样做,您可能希望使用UIGestureRecognizer而不是touchesBegan等等 - 您可以在使用时更轻松地设置更复杂的方案。但是手势识别器并非在所有版本的iOS上都可用。

或者,实现看起来像按钮或任何你想要的东西,并使用touchesBegan等处理它的触摸(以及视图拖动等) - 即不要使用实际的按钮。

答案 1 :(得分:0)

如果有人遇到类似的问题,我会发布我使用过的解决方案。我覆盖了我用作按钮的另一个UIView类。在每个touchedBegan/Moved/Ended中,我实现了按钮单击所需的代码,然后将命令链的触摸器传递给下一个自定义UIView,如下所示:

- (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];
}

请注意,我使用NSNotifications将操作发送到包含已覆盖视图的ViewController