UIButton delayed state change

后端 未结 5 1630
庸人自扰
庸人自扰 2020-12-31 03:31

I have a UIButton subview inside of a UITableViewCell.

When this button is touched, the user must hold the button for about a half second f

相关标签:
5条回答
  • 2020-12-31 03:56

    I just change the image from within the target action method:

    [sender setBackgroundImage:[UIImage imageNamed:@"highlighted-image.png"] forState:UIControlStateNormal];
    

    It changes the background image instantly.

    0 讨论(0)
  • 2020-12-31 03:59

    The problem is that your UIButton is inside a UITableView. This means that the table view has to determine whether your tap is going to be a swipe or if it's just a tap intended for the button. The table view has to delay sending a message to the UIButton until it knows that the user doesn't intend to swipe and therefore scroll the view instead of pressing the button.

    If you don't need a table view, get rid of the UITableView.

    0 讨论(0)
  • 2020-12-31 04:03

    Edit: completely re-written following a misunderstanding of the question

    One way of thinking of a UIButton is as a shorthand way of setting up an area of the screen that can respond to various instantaneous touch events the response it makes is defined by UIControl's Target-Action system for delivering messages to other objects.

    UIControlEventTouchDown sounds like the one you need to respond to. It will be triggered as soon as someone touches inside your button - this is what the "Contact Info" button in SMS does.

    UIButton* myButton = [[UIButton buttonWithType:UIButtonTypeRoundedRect]; // SEt up title, frame etc [myButton addTarget:self action:@selector(myButtonWasPressed) forControlEvents: UIControlEventTouchDown]; [myMainView addSubView:myButton];

    Will send a -(void)myButtonWasPressed message to the object this code runs from (ideally you view controller). In myButtonWasPressed you can then add a new view or take any action you like. The SMS app pushes a view controller to display the contact info using a navigation controller.

    If this still doesn't solve your problem, you're going to have to post some code in order to get more insight into what's going wrong.

    0 讨论(0)
  • 2020-12-31 04:07

    I just encountered this problem and saw that this issue hadn't been closed. After screwing around for a while I found a fix for it.

    Now you can fix this by turning off delaysContentTouches or unchecking the "Delays content touches" box on the tableview.

    The only negative side effect is that the user won't be able to tap down on a button and initiate a scrolling gesture. However, if the user tries to scroll starting from anywhere that doesn't itself accept touches, the behavior should be the same as before.

    0 讨论(0)
  • 2020-12-31 04:16

    Up for David Hodge's answer.

    I just want to add a way to remove that "only negative side effect", already described by David: if you start scrolling inside a UIcontrol in a UIScrollView with delayContentTouches=NO, scrolling doesn't work.

    SOLUTION Subclass UIScrollView (or UITableView as the original question) and override:

    -(BOOL) touchesShouldCancelInContentView:(UIView *)view {   
    return YES; 
    }
    

    Your UIControls inside UIScrollView/UITableView will change their state immediately on tap and the scrollviews will be able to scroll even if the touch starts on some UIControl. Works like a charm.

    0 讨论(0)
提交回复
热议问题