mat*_*att 1 objective-c event-handling uibutton ios
我在视图上创建一个UIButton,我想让touchesMoved只控制UIButton,而不是整个视图
-(void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event{
UITouch *touch = [touches anyObject];
CGPoint touchMoved = [touch locationInView:self.view];
}
Run Code Online (Sandbox Code Playgroud)
如果我触摸UIButton,我想这样做,然后可以用我的手指移动UIButton,如果我触摸其他视图并且我的手指在屏幕上移动,则UIButton什么都不做.这意味着函数touchesMoved只具有UIButton的作用,那么我该怎么做呢?谢谢
小智 5
我假设您显示的代码发生在您的自定义视图控制器子类中,并且它UIButton是其视图的子视图.
BOOL在您的班级中定义一个您设置为NO第一个的简单.然后在事件处理方法中更新它.
// .h
BOOL buttonTouched;
// .m
// in the viewDidLoad
buttonTouched = NO;
-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
// test wether the button is touched
UITouch *touch = [touches anyObject];
CGPoint touchBegan = [touch locationInView:self.view];
if(CGRectContainsPoint(theButton.frame, touchBegan) {
buttonTouched = YES;
}
}
-(void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event {
if(buttonTouched) {
// do it here
UITouch *touch = [touches anyObject];
CGPoint touchMoved = [touch locationInView:self.view];
CGRect newFrame = CGRectMake(touchMoved.x,
touchMoved.y,
theButton.frame.width,
theButton.frame.height);
theButton.frame = newFrame;
}
}
// when the event ends, put the BOOL back to NO
-(void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event {
buttonTouched = NO;
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
1151 次 |
| 最近记录: |