标签:
majorRadiusTolerance
property 来调整得到准确的数值。@interface ViewController () @property (nonatomic, weak) UIView *redView; @end - (void)viewDidLoad { [super viewDidLoad]; // 1. 实例化红色的视图 UIView *view = [[UIView alloc] initWithFrame:CGRectMake(110, 100, 100, 100)]; view.backgroundColor = [UIColor redColor]; [self.view addSubview:view]; self.redView = view; } #pragma mark - 移动 - (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event { NSLog(@"touchesMoved"); // 1. 获取到集合中的触摸对象 UITouch *touch = [touches anyObject]; // 2. 获取手指所在的位置 CGPoint location = [touch locationInView:self.view]; // 根据运行发现,手指第一次移动时,红色视图会出现跳跃的情况,会影响用户的使用 // 1) 取出前一次的手指位置 CGPoint preLocation = [touch previousLocationInView:self.view]; // 2) 计算两次手指之间的距离差值 CGPoint offset = CGPointMake(location.x - preLocation.x, location.y - preLocation.y); // 3) 修正红色视图的中心点 CGPoint center = CGPointMake(_redView.center.x + offset.x, _redView.center.y + offset.y); // 3. 设置红色视图的位置 self.redView.center = center; }
@interface MultiTouchViewController () @property (nonatomic, strong) NSArray *images; @end - (void)viewDidLoad { [super viewDidLoad]; // 初始化图像 _images = @[[UIImage imageNamed:@"spark_blue"], [UIImage imageNamed:@"spark_red"]]; // 让视图支持多选,如果不设置多点选项,默认只支持单点 [self.view setMultipleTouchEnabled:YES]; } #pragma mark - 触摸事件 - (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event { NSInteger i = 0; for (UITouch *touch in touches) { // 2. 取出手指触摸位置 CGPoint location = [touch locationInView:self.view]; // 3. 在相应位置绘制图像 UIImageView *imageView = [[UIImageView alloc] initWithImage:_images[i]]; imageView.center = location; // 4. 添加到视图 [self.view addSubview:imageView]; // 5. 添加动画,让图像的透明度降低,然后消失 [UIView animateWithDuration:1.0f animations:^{ imageView.alpha = 0.5f; } completion:^(BOOL finished) { // 将图像视图从视图中删除 [imageView removeFromSuperview]; }]; i++; } }
1 - (BOOL)canBecomeFirstResponder 2 { 3 return YES; 4 } 5 6 - (void)viewDidAppear:(BOOL)animated 7 { 8 [self becomeFirstResponder]; 9 } 10 11 - (void)viewDidDisappear:(BOOL)animated 12 { 13 [self resignFirstResponder]; 14 } 15 16 #pragma mark 摇晃开始事件 17 - (void)motionBegan:(UIEventSubtype)motion withEvent:(UIEvent *)event 18 { 19 if (motion == UIEventSubtypeMotionShake) { 20 NSLog(@"摇晃了"); 21 } 22 }
标签:
原文地址:http://www.cnblogs.com/kangshang/p/4418281.html