)viewDidLoad://视图控制器将视图载入内存后会调用此方法
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
//NSLog(@"%@接收了事件", [self class]);
//[self.nextResponder.nextResponder touchesBegan:touches withEvent:event];
[[self viewController] touchesBegan:touches withEvent:event];
}
//确保响应者是viewController
- (UIViewController *)viewController {
UIResponder *responder = self.nextResponder;
do {
//isKindOfClass:表示当前对象是某个类或者是其子类的实例对象
//isMemberOfClass:表示当前对象是某个类的实例对象
if([responder isKindOfClass:[UIViewController class]]) {
return (UIViewController *)responder;
}
responder = responder.nextResponder;
} while (responder != nil);//二、do-while语句的一般形式为:
// do语句//while(表达式);
// 这个循环与while循环的不同在于:它先执行循环中的语句,然后再判断表达式是否为真, 如果为真则继续循环;如果为假, 则终止循环。因此, do-while循环至少要执行一次循环语句。
// 用while语句应注意以下几点:
// 1、while语句中的表达式一般是关系表达或逻辑表达式,只要表达式的值为真(非0)即可继续循环。
// 2、循环体如包括有一个以上的语句,则必须用{}括起来,组成复合语句。
return nil;
}
---------------------------手势--------------------------
#import "ViewController.h"
@interface ViewController () {
UIImageView *_imgView;
CGFloat scale;
}
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
scale = 1;
// Do any additional setup after loading the view, typically from a nib.
_imgView = [[UIImageView alloc] initWithFrame:self.view.bounds];
_imgView.image = [UIImage imageNamed:@"feng.jpeg"];
_imgView.userInteractionEnabled = YES;
[self.view addSubview:_imgView];
//1.单击
UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapAction:)];
[_imgView addGestureRecognizer:tap];
// 2.双击
UITapGestureRecognizer *doubleTap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(doubleTapAction:)];
doubleTap.numberOfTapsRequired = 2;
[_imgView addGestureRecognizer:doubleTap];
//双击失败才算单击
[tap requireGestureRecognizerToFail:doubleTap];
// 3.长按
UILongPressGestureRecognizer *longPress = [[UILongPressGestureRecognizer alloc] initWithTarget:self action:@selector(longPressAction:)];
longPress.minimumPressDuration = 1.5;//长按时间
[_imgView addGestureRecognizer:longPress];
// 4.轻扫
UISwipeGestureRecognizer *swipe = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(swipeAction:)];
swipe.direction = UISwipeGestureRecognizerDirectionRight | UISwipeGestureRecognizerDirectionDown;
//轻扫方式,右,下
[_imgView addGestureRecognizer:swipe];
// 5.移动
// 6.旋转
UIRotationGestureRecognizer *rotation = [[UIRotationGestureRecognizer alloc] initWithTarget:self action:@selector(rotationAction:)];
[_imgView addGestureRecognizer:rotation];
// 7.捏合
UIPinchGestureRecognizer *pin = [[UIPinchGestureRecognizer alloc] initWithTarget:self action:@selector(pinAction:)];
_imgView.transform = CGAffineTransformMakeScale(scale, scale);
[_imgView addGestureRecognizer:pin];
}
- (void)tapAction:(UITapGestureRecognizer *)tap {
NSLog(@"单击");
}
- (void)doubleTapAction:(UITapGestureRecognizer *)tap {
NSLog(@"双击");
}
- (void)longPressAction:(UILongPressGestureRecognizer *)longPress {
if (longPress.state == UIGestureRecognizerStateBegan) {
NSLog(@"长按开始");
} else if (longPress.state == UIGestureRecognizerStateEnded) {
NSLog(@"长按结束");
}
}
- (void)swipeAction:(UISwipeGestureRecognizer *)swipe {
NSLog(@"轻扫");
}
- (void)rotationAction:(UIRotationGestureRecognizer *)roation {
if (roation.state == UIGestureRecognizerStateChanged) {
//让图随着手势进行旋转
CGFloat angle = roation.rotation;
roation.view.transform = CGAffineTransformMakeRotation(angle);
} else if (roation.state == UIGestureRecognizerStateEnded) {
[UIView animateWithDuration:0.3 animations:^{
roation.view.transform = CGAffineTransformIdentity;
}];
}
}
- (void)pinAction:(UIPinchGestureRecognizer *)pin{
if (pin.state == UIGestureRecognizerStateEnded) {//捏合手势结束时
scale = 1;
_imgView.transform = CGAffineTransformMakeScale(scale, scale);
return;
}
if (pin.scale > 1) {//捏合方向,因为不停的话等于一直调用这个方法
NSLog(@"放大");
scale = scale + 0.1;
_imgView.transform = CGAffineTransformMakeScale(scale, scale);
}
else{
NSLog(@"缩小");
scale = scale - 0.1;
_imgView.transform = CGAffineTransformMakeScale(scale, scale);
}
}
---------------------------摇动手势------------------------
- (void)motionBegan:(UIEventSubtype)motion withEvent:(UIEvent *)event {
if (motion == UIEventSubtypeMotionShake) {
NSLog(@"摇到了一个大红包");
}
}
- (void)motionEnded:(UIEventSubtype)motion withEvent:(UIEvent *)event {
NSLog(@"摇晃事件结束");
}
- (BOOL)canBecomeFirstResponder {//在自定义的UIView子类中,需要实现canBecomeFirstResponder方法,并返回YES(默认返回FALSE),才可使becomeFirstResponder可返回YES,才可使其成为第一响应者,即接受第一响应者状态。
// 一个响应者只有当当前响应者可以取消第一响应者状态 (canResignFirstResponder) 并且新的响应者可以成为第一响应者时,才可以成为第一响应者。
return YES;
}