我有2 UIButton
个,每个都改变了中间的数字(请参见图片)。
我希望当用户按住按钮时,数字仍然会改变(现在,当你想要从100改为200你需要点击100次而且它不好时,我希望当你按住按钮时数字将会改变),这可能意味着应该一次又一次地调用每个UIButon
的动作,直到用户释放按钮。
我该怎么做?谢谢!
答案 0 :(得分:0)
你可以在Swift中这样做:
@IBOutlet weak var btn: UIButton!
override func viewDidLoad() {
let tapGesture = UITapGestureRecognizer(target: self, action: #selector(ViewController.Long)) //Tap function will call when user tap on button
let longGesture = UILongPressGestureRecognizer(target: self, action: #selector(ViewController.Long)) //Long function will call when user long press on button.
tapGesture.locationInView(btn);
btn.addGestureRecognizer(tapGesture) // To set the tap gesture recognizer: the function Tap () will be calling when the user select the button normally.
btn.addGestureRecognizer(longGesture) // To set the long gesture recognizer: the function Long () will be calling when the user select the button with a long press.
}
func Tap() {
print("Tap happend")
}
func Long() {
print("Long press")
}
答案 1 :(得分:0)
你必须使用UILongPressGestureRecognizer这是一个连续的调用动作,而不是通过按钮点击调用方法。所以只需将左/右按钮放在不同的视图中,并将UILongPressGestureRecognizer添加到这些左/右视图中,并将目标添加到这些识别器,例如
{{1}}
希望这能解决您的问题。
答案 2 :(得分:0)
在UIButton上使用UILongPressGestureRecognizer
UILongPressGestureRecognizer *LeftButtonPress = [[UILongPressGestureRecognizer alloc] initWithTarget:self action:@selector(longPressHandler:)];
UILongPressGestureRecognizer *RightButtonPress = [[UILongPressGestureRecognizer alloc] initWithTarget:self action:@selector(longPressHandler:)];
[btnLeft addGestureRecognizer:LeftButtonPress];
[btnRight addGestureRecognizer:RightButtonPress];
更新屏幕的状态更改代码。
- (void) longPressHandler: (UILongPressGestureRecognizer *) gesture {
if(gesture.state == UIGestureRecognizerStateChanged) {
// update number on screen
}
}
答案 3 :(得分:0)
这是我编写的符合您要求的代码。确保在故事板中链接加号和减号按钮插座及其目标事件(内部触摸)。如果不清楚,请告诉我。
#import "ViewController.h"
@interface ViewController () {
NSTimer *speedTimer;
}
//Link this in your storyboard Outlets
@property (weak, nonatomic) IBOutlet UILabel *speedUILabel;
@property (weak, nonatomic) IBOutlet UIButton *plusUIButton;
@property (weak, nonatomic) IBOutlet UIButton *minusUIButton;
//Link this in your storyboard events respectively
- (IBAction)plusUIButton:(id)sender;
- (IBAction)minusUIButton:(id)sender;
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
UILongPressGestureRecognizer *plusButtonPress = [[UILongPressGestureRecognizer alloc] initWithTarget:self action:@selector(longPressHandler:)];
UILongPressGestureRecognizer *minusButtonPress = [[UILongPressGestureRecognizer alloc] initWithTarget:self action:@selector(longPressHandler:)];
[_plusUIButton addGestureRecognizer:plusButtonPress];
[_minusUIButton addGestureRecognizer:minusButtonPress];
plusButtonPress.delegate = self;
minusButtonPress.delegate = self;
plusButtonPress.minimumPressDuration = 0.5;
minusButtonPress.minimumPressDuration = 0.5;
plusButtonPress.allowableMovement = 0.5;
minusButtonPress.allowableMovement = 0.5;
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
- (IBAction)plusUIButton:(id)sender {
NSInteger plus = [_speedUILabel.text integerValue] + 1;
if (plus >= 0) {
_speedUILabel.text = [NSString stringWithFormat:@"%ld", (long)plus];
}
}
- (IBAction)minusUIButton:(id)sender {
NSInteger minus = [_speedUILabel.text integerValue] - 1;
if (minus >= 0) {
_speedUILabel.text = [NSString stringWithFormat:@"%ld", (long)minus];
}
}
- (void) longPressHandler: (UILongPressGestureRecognizer *) gesture {
if (gesture.view == _plusUIButton) {
if(gesture.state == UIGestureRecognizerStateBegan) {
speedTimer = [NSTimer scheduledTimerWithTimeInterval:0.01 target:self selector:@selector(plusUIButton:) userInfo:nil repeats:true];
}
else if(gesture.state == UIGestureRecognizerStateEnded) {
[speedTimer invalidate];
speedTimer = nil;
}
}
else if (gesture.view == _minusUIButton) {
if(gesture.state == UIGestureRecognizerStateBegan) {
speedTimer = [NSTimer scheduledTimerWithTimeInterval:0.01 target:self selector:@selector(minusUIButton:) userInfo:nil repeats:true];
}
else if(gesture.state == UIGestureRecognizerStateEnded) {
[speedTimer invalidate];
speedTimer = nil;
}
}
}
@end