2 mapmapm MapMapM 于 2013.03.28 10:11 提问

objective-C中触发事件

需要实现如下功能:将可拖拽的按钮拖拽到屏幕的指定位置时,自动触发一个事件。不知道应该从哪儿入手,请高人指教,谢谢。

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;
}

1个回答

h3c4lenovo
h3c4lenovo   2013.03.28 10:53
已采纳
    //添加拖拽停止监听
[btn1 addTarget:self action:@selector(imageMovedEnd:withEvent:) forControlEvents:UIControlEventTouchUpInside];

//拖拽停止处理
- (IBAction) imageMovedEnd:(id) sender withEvent:(UIEvent *) event
{

    CGPoint point = [[[event allTouches] anyObject] locationInView:self.view];
    UIControl *control = sender;
    control.center = point;
    NSLog(@"End... %f,%f",point.x,point.y);
    // do  sth..  判断Point是否在屏幕的指定位置即可,简单的碰撞检测,可以自己去Google搜一下
}
MapMapM
MapMapM 多谢
大约 5 年之前 回复
Csdn user default icon
上传中...
上传图片
插入图片
准确详细的回答,更有利于被提问者采纳,从而获得C币。复制、灌水、广告等回答会被删除,是时候展现真正的技术了!