1. UIImageView的初始化和设置图片
//初始化
UIImageView *imageView = [UIImageView alloc]initWithFrame:CGRectMake(50,50,50,50)];
设置图片
[imageView setImage:[UIImage imageNamed:@""]];//第一种针对是本地的单个图片
[imageView setImage:[UIImae imageWithContentOfFile:[NSBundle mainBundle] pathForResource: ofType:]
]];
//第三种
NSData *data = [NSData dataWithContentOfFile:[NSBundle mainBundle] pathForResource: ofType:];
UIImage *image = [UIImage imageWithData:data];
[imageView setImage:image];//一般式请求网络上的数据。
2. 手势识别器
手势识别器有7个子类,分别是轻拍手势、平移手势、轻扫手势、缩放手势、旋转手势、长按手势、屏幕边界平移手势。
UITapGestureRecoginizer 是轻拍手势识别器,能识别轻拍操作。
UILongPressGestureRecoginizer 是长按手势识别器,能识别长按操作
UIRotationCestureRecoginizer 是旋转手势识别器,能识别旋转操作
UIPitchGestureRecoginizer 是捏合手势识别器,能识别捏合操作
UIPanGestureRecoginizer 是平移手势识别器,能识别拖拽操作
UISwipeGestureRecoginizer 是清扫手势识别器,能识别拖拽操作
UIScreenEdgePanGestureRecognizer 是屏幕边缘清扫识别器,是iOS7中新增加的一个
3.使用手势识别器的方法
分为两步:创建手势实例,指定一个回调方法,当手势开始、改变、或结束时,回调方法被调用
添加到view上,每个手势只对于一个View,但每个view可以对应对个手势
3.1 对图片进行拖拽的代码
UIImageView *imageView = [[UIImageView alloc]initWithImage:[UIImage imageNamed:@"1.jpg"]];
imageView.frame = CGRectMake(50, 50, 100, 150);
//创建手势实例平移
UIPanGestureRecognizer *pan = [[UIPanGestureRecognizer alloc]initWithTarget:self action:@selector (panAction:)];
//添加到Imageview上
[imageView addGestureRecognizer:pan];
//创建手势缩放的
UIPinchGestureRecognizer *pinch = [[UIPinchGestureRecognizer alloc]initWithTarget:self action:@selector(pinchAction:)];
[imageView addGestureRecognizer:pinch];
//添加手势旋转
UIRotationGestureRecognizer *rotation = [[UIRotationGestureRecognizer alloc]initWithTarget:self action:@selector(rotationAction:)];
[imageView addGestureRecognizer:rotation];
self.view.backgroundColor = [UIColor whiteColor];
//打开交互,不然不会响应
imageView.userInteractionEnabled = YES;
[self.view addSubview:imageView];
//回调的方法平移的
- (void)panAction:(UIPanGestureRecognizer *)recoginize
{
CGPoint translation = [recoginize translationInView:self.view];
recoginize.view.center = CGPointMake(recoginize.view.center.x + translation.x, recoginize.view.ce
nter.y + translation.y);
[recoginize setTranslation:CGPointZero inView:self.view];
}
//回调方法缩放
- (void)pinchAction:(UIPinchGestureRecognizer *)recoginze
{
recoginze.view.transform = CGAffineTransformScale(recoginze.view.transform, recoginze.scale, recoginze.scale);
recoginze.scale = 1;
}
//旋转的手势的方法
- (void)rotationAction:(UIRotationGestureRecognizer *)recoginze
{
recoginze.view.transform = CGAffineTransformRotate(recoginze.view.transform, recoginze.rotation);
recoginze.rotation = 0;
}
4. 手势之间是互斥的,如果你想同时出发多个的话,必须实现一个协议UIGestureRecognizerDelegate
@interface ViewController : UIViewController<UIGestureRecognizerDelegate>
@end
并在协议里的方法返回YES
-(BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldRecognizeSimultaneouslyWi
thGestureRecognizer:(UIGestureRecognizer *)otherGestureRecognizer
{
return YES;
}
把self作为代理设置给手势
pan.delegate = self;
pinch.delegate = self;
rotation.delegate = self;
//