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

 

你可能感兴趣的:(ios,拖动,按钮,点击)