码迷,mamicode.com
首页 > 移动开发 > 详细

IOS拖动

时间:2015-01-02 22:21:27      阅读:293      评论:0      收藏:0      [点我收藏+]

标签:

#import "ViewController.h"

@interface ViewController ()

@property (nonatomic, strong) UIButton *btn;

@end

@implementation ViewController
@synthesize btn;

- (void)viewDidLoad
{
[super viewDidLoad];
self.btn = [UIButton buttonWithType:UIButtonTypeRoundedRect];
self.btn.frame = CGRectMake(10, 10, 50, 50);

[self.btn setTitle:@"触摸" forState:UIControlStateNormal];
[self.btn setTitle:@"移动" forState:UIControlEventTouchDown];
[self.btn addTarget:self action:@selector(dragMoving:withEvent: )forControlEvents: UIControlEventTouchDragInside];
[self.btn addTarget:self action:@selector(dragEnded:withEvent: )forControlEvents: UIControlEventTouchUpInside |
UIControlEventTouchUpOutside];

[self.view addSubview:self.btn];
}


- (void) dragMoving: (UIControl *) c withEvent:ev
{
c.center = [[[ev allTouches] anyObject] locationInView:self.view];
}

- (void) dragEnded: (UIControl *) c withEvent:ev
{
c.center = [[[ev allTouches] anyObject] locationInView:self.view];
}

@end

IOS拖动

标签:

原文地址:http://www.cnblogs.com/wcLT/p/4198882.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!