将可拖动按钮拖动到屏幕上的某个点时,如何触发事件?
UIButton *btn1 = [UIButton buttonWithType:UIButtonTypeRoundedRect];
[btn1 setFrame:CGRectMake(10.0f, 300.0f, 300.0f, 42.0f)];
[btn1 setTitle:[NSString stringWithFormat:@"View Fullscreen Decor"] forState:UIControlStateNormal];
[btn1 addTarget:self action:@selector(imageMoved:withEvent:) forControlEvents:UIControlEventTouchDragInside];
[self.view addSubview:btn1];
[super viewDidLoad];
[myScrollView addSubview:btn1];
- (IBAction) imageMoved:(id) sender withEvent:(UIEvent *) event
{
CGPoint point = [[[event allTouches] anyObject] locationInView:self.view];
UIControl *control = sender;
control.center = point;
}
最佳答案
- (IBAction) imageMoved:(id) sender withEvent:(UIEvent *) event{
CGPoint touchPoint = [[[event allTouches] anyObject] locationInView:self.view];
UIControl *control = sender;
control.center = touchPoint;
CGRect rectToCompare = location you want on your super view;
if (CGRectContainsPoint(rectToCompare, touchPoint)) {
//trigger an event.
}
}
关于iphone - 将UIButton拖动到屏幕的某个点时触发事件? objective-c ,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/15668448/