尝试将您想要触摸的按钮实现为"触摸向下","触摸内部"和"触摸向外"按钮.
UIButtons可以响应许多不同类型的事件触摸取消触摸向下触摸向下重复触摸拖动输入触摸拖动退出触摸拖动内部触摸拖动外部触摸向上触摸向上外部
您可以为每个按钮为每个按钮实现不同的操作代码,以便最好地控制您想要的任何内容.简单的情况仅使用上面提到的2.
这个代码已经过测试并且有效
在你的ViewController头文件中(这是我的):
@interface ViewController : UIViewController{
IBOutlet UIButton * upButton; // count up when finger released button
IBOutlet UIButton * downButton;
IBOutlet UILable * score;
BOOL isButtonDown;
unsigned int youCounter;
}
-(IBAction)downButtonDown:(id)sender;
-(IBAction)downButtonUpInside:(id)sender;
-(IBAction)downButtonDragOutside:(id)sender event:(UIEvent *)event;
-(IBAction)downButtonUpOutside:(id)sender event:(UIEvent *)event;
@end
Run Code Online (Sandbox Code Playgroud)
在.xib中,将向下按钮(您希望成为原始手指按下按钮的按钮)连接到上面的正确操作.
在ViewController.m文件中
-(void)viewDidLoad{
[super viewDidLoad];
isButtonDown = NO;
youCounter = 0;
}
-(IBAction)downButtonDown:(id)sender{
isButtonDown = YES;
}
-(IBAction)downButtonUpInside:(id)sender{
isButtonDown = NO;
}
-(IBAction)downButtonDragOutside:(id)sender event:(UIEvent *)event{
NSArray theTouches = [[event allTouches] allObjects];
[downButton setHighlighted:YES];
if(YES == [upButton pointInside:[[theTouches objectAtIndex:0] locationInView:upButton] withEvent:event]){
[upButton setHighlighted:YES];
}else{
[upButton setHighlighted:NO];
}
}
-(IBAction)downButtonUpOutside:(id)sender event:(UIEvent *)event{
if(YES == [upButton pointInside:[[theTouches objectAtIndex:0] locationInView:upButton] withEvent:event]){
youCounter++;
score.text = [NSString stringWithFormat:@"Score = %d", youCounter];
}
[downButton setHighlighted:NO];
[upButton setHighlighted:NO];
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
10126 次 |
| 最近记录: |