【问题标题】:How to let UIButton is highlighted when I touch outside to inside bound?当我从外到内触摸时如何让 UIButton 突出显示?
【发布时间】:2014-08-09 05:12:31
【问题描述】:

我触摸 UIButton 的外部边界,然后在 UIButton 内拖动。状态未突出显示。

当我的手指刚刚触摸到 UIButton 的边界内时,如何设置 UIButton 被检测到突出显示?

例如:制作一个钢琴应用程序,当我触摸一个音符并向左或向右拖动时,它不仅会检测第一个声音,它还会检测我触摸的所有声音。

【问题讨论】:

    标签: ios objective-c uibutton


    【解决方案1】:

    尝试使用它。

      UIButton *myButton = [[UIButton alloc]initWithFrame:CGRectMake(5, 5, 50, 50)];
      myButton.backgroundColor = [UIColor greenColor];
    myButton addTarget:self action:@selector(myFunction) forControlEvents:UIControlEventTouchDragEnter; //this method is fired when user touches the button and drags it from inside the bounds of the button.
    
    -(IBAction)myFunction:sender(id){
        UIButton *myButton = (UIButton*)sender;
        myButton.backgroundColor = [UIColor redColor];
         myButton.highlighted = YES;
         [self checkForHighlight];
    }
    
    -(void)checkForHighlight {
       if(myButton.highlighted){
         myButton.backgroundColor = [UIColor blueColor];
        }
       }
    

    您需要将 UIButton *myButton 声明为静态变量,以便 VC 的所有方法都可以访问它。

    【讨论】:

    • 谢谢。但我不需要 IBAction。我使用 TouchDragEnter 不能让按钮在从外部拖动到边界内时突出显示。它只是发送一个动作,但我需要突出显示按钮状态
    猜你喜欢
    • 2014-02-03
    • 2012-12-14
    • 2015-08-28
    • 2014-09-10
    • 1970-01-01
    • 1970-01-01
    • 2011-05-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多