簡體   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