---
title: "IOS按钮拖动和点击"
author: "吃饱了就饿"
date: "2013-06-09T09:49:34Z"
tags: []
categories: ["移动开发"]
slug: "ios"
draft: false
lastmod: "2013-06-09T09:49:34Z"
id: 1884704
url: "https://dxldy.iteye.com/blog/1884704"
---

按钮初始化

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