繁体   English   中英

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

[英]need UIButton inside custom UIView respond to touches

我有一些按钮作为子视图的自定义UIView。 我正在使用touchesBegan / Moved / Ended来用用户的手指旋转视图。 问题是,当我尝试从按钮拖动/旋转视图时,不会调用touches方法。 我需要按钮来响应UIControlEventTouchUpInside事件,但是如果将其与视图一起拖动,则使视图旋转。 我已经看过这个问题和这个问题,但是找不到任何一种对我有用的解决方案。

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

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

有什么建议么?

也许不是您要找的答案,但是:

我不确定为什么要通过拖动后的视图将“手指按下按钮然后将手指拖动到按钮外部”活动作为拖动/旋转动作。 就我而言,如果您将手指放在按钮内,则表示您正在与按钮交互,而不是背后的视图; 并且如果您仍在触摸屏幕的同时将手指从按钮上移开,则是为了中止按钮的按下(即您已经“武装”了它,但没有“触发”它),而不与后面的视图进行交互。

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

如果您绝对想这样做,则可以考虑使用UIGestureRecognizer而不是touchesBegantouchesBegan使用它们时,您可以更轻松地设置更复杂的场景。 但是手势识别器并非在所有版本的iOS上都可用。

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

万一有人遇到类似问题,我将发布我使用的解决方案。 我覆盖了另一个用作按钮的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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM