好久没做整理了,在忙起来之前赶紧把整理做好,方便自己复习和以后的使用.Fighting!
首先创建RootViewController,做好准备工作.然后在RootViewController.m文件写一个属性
@property(nonatomic,retain)UIImageView *imageView;
然后通过属性,创建视图
self.imageView = [[UIImageViewalloc] initWithFrame:CGRectMake(100,100, 200, 200)];
self.imageView.backgroundColor = [UIColoryellowColor];
[self.viewaddSubview:self.imageView];
[_imageView release];
self.imageView.image = [UIImageimageNamed:@"1.jpg"];
NSLog(@"%@",self.imageView);
在这做个打印,会显示userInteractionEnabled = NO,userInteractionEnabled叫做用户交互.如果用户交互没有打开,无法通过手势的方法跟它进行交互.如果不确定交互开没开,可以打印一下,没打开交互的就会显示NO;
// 打开用户交互
self.imageView.userInteractionEnabled =YES;
// 1.点击
// 初始化方法
UITapGestureRecognizer *tap = [[UITapGestureRecognizeralloc]initWithTarget:selfaction:@selector(tapAction:)];
// 把手势添加到视图上
[self.imageViewaddGestureRecognizer:tap];
// 内存管理
[tap release];
// 触发方法需要点击几次,默认是1
tap.numberOfTapsRequired =2;
// 模拟手指
tap.numberOfTouchesRequired =2;
// 1. 轻点的点击方法实现
- (void)tapAction:(UITapGestureRecognizer *)tap{
NSLog(@"点击了");
}
// 2.长按
// 初始化方法
UILongPressGestureRecognizer *longPress = [[UILongPressGestureRecognizeralloc] initWithTarget:selfaction:@selector(longPress:)];
[self.imageViewaddGestureRecognizer:longPress];
[longPress release];
// 设置触发的最短时间
longPress.minimumPressDuration =2;
// 设置可移动的范围
longPress.allowableMovement = 300;
// 2. 长按的方法实现
- (void)longPress:(UILongPressGestureRecognizer *)longPress{
NSLog(@"长按");
// 可以按照对应的手势状态来完成判断
if (longPress.state ==UIGestureRecognizerStateBegan) {
NSLog(@"开始了");
}
}
// 3. 旋转手势
UIRotationGestureRecognizer *rotation = [[UIRotationGestureRecognizeralloc] initWithTarget:selfaction:@selector(rotation:)];
[self.imageViewaddGestureRecognizer:rotation];
[rotation release];
// 3. 旋转手势方法实现
- (void)rotation:(UIRotationGestureRecognizer *)rotation{
NSLog(@"旋转");
// 图片随手势一同旋转
self.imageView.transform =CGAffineTransformRotate(self.imageView.transform, rotation.rotation);
rotation.rotation = 0;
}
// 4. 捏合
UIPinchGestureRecognizer *pinch = [[UIPinchGestureRecognizeralloc] initWithTarget:selfaction:@selector(pinchAction:)];
[self.imageViewaddGestureRecognizer:pinch];
[pinch release];
// 4. 捏合方法实现
- (void)pinchAction:(UIPinchGestureRecognizer *)pinch{
NSLog(@"捏合");
// 实现图片随手势捏合
self.imageView.transform =CGAffineTransformScale(self.imageView.transform, pinch.scale, pinch.scale);//第一个,是以它作为图片捏合的起点
pinch.scale = 1;
}
// 5. 拖拽
UIPanGestureRecognizer *pan = [[UIPanGestureRecognizeralloc] initWithTarget:selfaction:@selector(panAction:)];
[self.imageViewaddGestureRecognizer:pan];
[pan release];
// 5. 拖拽手势实现
- (void)panAction:(UIPanGestureRecognizer *)pan{
NSLog(@"拖拽");
// 先根据手势找到经过的点
CGPoint point = [pan translationInView:self.imageView];
// 修改视图的transform
self.imageView.transform =CGAffineTransformTranslate(self.imageView.transform, point.x, point.y);
[pan setTranslation:CGPointZeroinView:self.imageView];
}
// 6. 轻扫手势
UISwipeGestureRecognizer *swipe = [[UISwipeGestureRecognizeralloc] initWithTarget:selfaction:@selector(swipeAction:)];
[self.imageViewaddGestureRecognizer:swipe];
[swipe release];
// 6. 轻扫手势方法实现
- (void)swipeAction:(UISwipeGestureRecognizer *)swipe{
NSLog(@"轻扫");
}
// 7. 屏幕边界手势
// 这个手势出现的最晚,7.0以后才出现
UIScreenEdgePanGestureRecognizer *screenEdge = [[UIScreenEdgePanGestureRecognizeralloc] initWithTarget:selfaction:@selector(screenEdgeAction:)];
screenEdge.edges = UIRectEdgeLeft;
[self.viewaddGestureRecognizer:screenEdge];
[screenEdge release];
// 7. 屏幕边界手势方法实现
- (void)screenEdgeAction:(UIScreenEdgePanGestureRecognizer *)screenEdge{
NSLog(@"边界");
// 视图背景颜色随机
self.view.backgroundColor = [UIColorcolorWithRed:arc4random()%256/255.0green:arc4random()%256/255.0blue:arc4random()%256/255.0alpha:1];
}
- (void)dealloc
{
[_imageView release];
[super dealloc];
}
///////////////// 扩展 ////////////
1 // 响应链
// 成为第一响应者,不点击就弹出键盘
[textField becomeFirstResponder];
// "nihao":"buhao"拼接成字符串
// NSString *str = @"\"nihao\":\"buhao\"";
// NSLog(@"%@",str);