[ios]气泡框按钮通过长按代码创建

标签: Xcode ios UIButton
发布时间: 2017/3/28 23:38:08
注意事项: 本文中文内容可能为机器翻译,如要查看英文原文请点击上面连接.

我知道如何创建泡芙,如果我的按钮添加到故事板,但如果我的按钮创建通过代码,如何可以创建气泡框。

UIButtonS *button = [UIButtonS buttonWithType:UIButtonTypeRoundedRect];            
[button addTarget:self action:@selector(siteButtonPressed:)forControlEvents:UIControlEventTouchUpInside];
[button setTitle:string1 forState:UIControlStateNormal];
button.frame = CGRectMake(XLocatioan, YLocation, 90, 30);

UILongPressGestureRecognizer *longPress = [[UILongPressGestureRecognizer alloc]
                                                   initWithTarget:self
                                                   action:@selector(handleLongPress:)];
longPress.minimumPressDuration = 1.0;
[button addGestureRecognizer:longPress];    
[self.view addSubview:button];

-  (void)handleLongPress:(UILongPressGestureRecognizer*)sender {
    if (sender.state == UIGestureRecognizerStateEnded) {
    }
    else if (sender.state == UIGestureRecognizerStateBegan){
      //create popover for button
    }
}

解决方法 1:

你已经在做正确的但你想。那里是没有需要检查的状态 gesture recognizer 。如果目标函数已被触发,它意味着用户已经进行了长时间的新闻。另外,请注意不是所有的属性状态的值可能会支持。正如文件所说︰ Some of these states are not applicable to discrete gestures

所以您的代码应如下所示 (除非您想要执行拖动或类似的东西)︰

UIButtonS *button = [UIButtonS buttonWithType:UIButtonTypeRoundedRect];            
[button addTarget:self action:@selector(siteButtonPressed:)forControlEvents:UIControlEventTouchUpInside];
[button setTitle:string1 forState:UIControlStateNormal];
button.frame = CGRectMake(XLocatioan, YLocation, 90, 30);

UILongPressGestureRecognizer *longPress = [[UILongPressGestureRecognizer alloc]
                                                   initWithTarget:self
                                                   action:@selector(handleLongPress:)];
longPress.minimumPressDuration = 1.0;
[button addGestureRecognizer:longPress];    
[self.view addSubview:button];

-  (void)handleLongPress:(UILongPressGestureRecognizer*)sender {
    //create popover for button        
}

如果你的目标是 iOS 6+ 您应该使用UIPopoverController来创建气泡框,否则请使用UIAlertView

赞助商