MAC OS X操作系统
Xcode编译器
创建工程项目和视图控制器 创建工程项目WhiteSpots白点,新建一个UIViewController。选中工程,右键-New File…选择“Cocoa Touch Class”-Next,给个合理的名称ViewController,再宿沫久Next完成。 在AppDelegate.m文件包含#import 'ViewController.h'。添加代码:self.window.rootViewController = [[ViewController alloc]init];将ViewController设置为根视图控制器。 为了美观,设置View的背景颜色为紫色。self.view.backgroundColor = [UIColor purpleColor]; 现在执行编译,run一下,效果如图。
添加白点及点击响应事件 选择UIButton、UIView、UIImageView作为白点都是可以的。当前使用UIButton,因为UIButton具有UIControl的属性,自带点击事件,也有背景图片。代召段码如下: UIButton *whiteSpotsButton = [UIButton buttonWithType:UIButtonTypeCustom]; whiteSpotsButton.frame = CGRectMake(10, 100, 44, 44); whiteSpotsButton.backgroundColor = [UIColor whiteColor]; whiteSpotsButton.layer.cornerRadius = 22; [whiteSpotsButton setTitle:@'白点' forState:UIControlStateNormal]; [whiteSpotsButton setTitleColor:[UIColor purpleColor] forState:UIControlStateNormal]; [whiteSpotsButton addTarget:self action:@selector(btnClick:) forControlEvents:UIControlEventTouchUpInside]; [self.view addSubview:whiteSpotsButton];#pragma mark- 按钮点击- (void)btnClick:(UIButton *)sender{ NSLog(@'白点被点击');}编译运行效果如图。
添加监听白点的手势 添加手势识别代理UIGestureRecognizerDelegate,创建UIPanGestureRecognizer拖动手势及其响应事件。代码如下: UIPanGestureRecognizer *panRecognizer = [[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(handlePanFrom:)]; [self.view bringSubviewToFront:whiteSpotsButton];//放到最前面 [whiteSpotsButton addGestureRecognizer:panRecognizer];//关键语句,添加一个手势监测; panRecognizer.maximumNumberOfTouches = 1; panRecognizer.delegate = self;-(void)handlePanFrom:(UIPanGestureRecognizer*)recognizer{ NSLog(@'拖动操作');}
代码下载地址:https://github.com/cjq002/WhiteSpots.git 去点击“Download ZIP”下载。
当前使用MAC OS X 版本为10.9.5
当前使用Xcode版本为6.0