程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> 更多編程語言 >> 編程綜合問答 >> ios-objective-C中觸發事件

ios-objective-C中觸發事件

編輯:編程綜合問答
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;
}

最佳回答:


    //添加拖拽停止監聽
[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搜一下
}
  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved