IOS按钮拖动和点击

按钮初始化

- (void)viewDidLoad
{
    [super viewDidLoad];
	// Do any additional setup after loading the view, typically from a nib.
    self.btn = [UIButton buttonWithType:UIButtonTypeRoundedRect];
    self.btn.frame = CGRectMake(10, 10, 50, 50);
    
    [self.btn setTitle:@"触摸" forState:UIControlStateNormal];
    [self.btn addTarget:self action:@selector(dragMoving:withEvent: )forControlEvents: UIControlEventTouchDragInside];
    [self.btn addTarget:self action:@selector(doClick:) forControlEvents:UIControlEventTouchUpInside];
    self.a=0;
    [self.view addSubview:self.btn];
}

 点击方法

-(void)doClick:(UIButton*)sender
{
    if (self.a==0)
    {
        NSLog(@"1111");
    }
    self.a=0;
}

 拖动方法

- (void) dragMoving: (UIButton *) c withEvent:ev
{
    self.a=1;
    c.center = [[[ev allTouches] anyObject] locationInView:self.view];
    NSLog(@"%f,,,%f",c.center.x,c.center.y);
}

猜你喜欢

转载自dxldy.iteye.com/blog/1884704