标签:
- (BOOL)canBecomeFirstResponder; // default is NO - (BOOL)becomeFirstResponder; - (BOOL)canResignFirstResponder; // default is YES - (BOOL)resignFirstResponder; - (BOOL)isFirstResponder;
// 手指触摸 - (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(nullable UIEvent *)event; // 手指移动 - (void)touchesMoved:(NSSet<UITouch *> *)touches withEvent:(nullable UIEvent *)event; // 手指移开 —> 这三个是一个完整的响应事件 - (void)touchesEnded:(NSSet<UITouch *> *)touches withEvent:(nullable UIEvent *)event; // 手指取消 —> 非正常手指移开(突然接电话什么时候会调用) - (void)touchesCancelled:(nullable NSSet<UITouch *> *)touches withEvent:(nullable UIEvent *)event;
// 触摸时间 @property(nonatomic,readonly) NSTimeInterval timestamp; @property(nonatomic,readonly) UITouchPhase phase; @property(nonatomic,readonly) NSUInteger tapCount; // touch down within a certain point within a certain amount of time @property(nonatomic,readonly) UITouchType type NS_AVAILABLE_IOS(9_0); // majorRadius and majorRadiusTolerance are in points // The majorRadius will be accurate +/- the majorRadiusTolerance @property(nonatomic,readonly) CGFloat majorRadius NS_AVAILABLE_IOS(8_0); @property(nonatomic,readonly) CGFloat majorRadiusTolerance NS_AVAILABLE_IOS(8_0); @property(nullable,nonatomic,readonly,strong) UIWindow *window; @property(nullable,nonatomic,readonly,strong) UIView *view; @property(nullable,nonatomic,readonly,copy) NSArray <UIGestureRecognizer *> *gestureRecognizers NS_AVAILABLE_IOS(3_2); - (CGPoint)locationInView:(nullable UIView *)view; - (CGPoint)previousLocationInView:(nullable UIView *)view;
- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event { // 1.单击,子view变成中心 UITouch *touch = touches.anyObject; // 获得现在的点 CGPoint nowPoint = [touch locationInView:self.superview]; self.center = nowPoint; } - (void)touchesMoved:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event { // 3.拖拽时候跟着鼠标走 UITouch *touch = touches.anyObject; // 获得当前的点 CGPoint previousPoint = [touch previousLocationInView:self.superview]; // 获得现在的点 CGPoint nowPoint = [touch locationInView:self.superview]; NSLog(@"%@",NSStringFromCGPoint(nowPoint)); NSLog(@"%@",NSStringFromCGPoint(previousPoint)); // 获取x,y偏移量 CGFloat x = nowPoint.x - previousPoint.x; CGFloat y = nowPoint.y - previousPoint.y; self.transform = CGAffineTransformTranslate(self.transform,x , y); } 主view - (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event { // 点击外面view时候,橘色view中心变变成点击点 UITouch *touch = touches.anyObject; self.orangeView.center = [touch locationInView:self]; }
- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event { NSLog(@"%@",self.imageArray); // 这个是有的,好的. for (UITouch *t in touches) { CGPoint nowPoint = [t locationInView:self.superview]; UIImageView *imgView = [[UIImageView alloc] initWithImage:[self ramdomImage]]; imgView.center = nowPoint; [self addSubview:imgView]; [UIView animateWithDuration:2.0 animations:^{ imgView.alpha = 0; } completion:^(BOOL finished) { [imgView removeFromSuperview]; }]; } } - (void)touchesMoved:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event { for (UITouch *t in touches) { CGPoint nowPoint = [t locationInView:self.superview]; UIImageView *imgView = [[UIImageView alloc] initWithImage:[self ramdomImage]]; imgView.center = nowPoint; [self addSubview:imgView]; [UIView animateWithDuration:2.0 animations:^{ imgView.alpha = 0; } completion:^(BOOL finished) { [imgView removeFromSuperview]; }]; } } - (UIImage *)ramdomImage { UIImage *image = self.imageArray[arc4random() % self.imageArray.count]; return image; } // 懒加载 - (NSArray *)imageArray { if (_imageArray == nil) { _imageArray = @[[UIImage imageNamed:@"spark_1"],[UIImage imageNamed:@"spark_2"],[UIImage imageNamed:@"spark_3"],[UIImage imageNamed:@"spark_4"],[UIImage imageNamed:@"spark_5"],[UIImage imageNamed:@"spark_6"],]; } return _imageArray; }
标签:
原文地址:http://www.cnblogs.com/aixiaoxin/p/4963603.html