猫猫分享,必须精品
原创文章,欢迎转载。转载请注明:翟乃玉的博客
地址:http://blog.csdn.net/u013357243?viewmode=contents
源码:http://blog.csdn.net/u013357243/article/details/45560213
效果
完成一个图片的捏合缩放,拖拽,旋转动作。
设计思路
拖拽:
首先是最简单的拖拽
//拖拽
-(void)panTest
{
UIPanGestureRecognizer *pan = [[UIPanGestureRecognizer alloc] init];
[self.iconView addGestureRecognizer:pan];
[pan addTarget:self action:@selector(panView:)];
}
- (void)panView:(UIPanGestureRecognizer *)pan
{
// 返回的值是以手指按下的点为原点
// 1 2 3 4 5
CGPoint point = [pan translationInView:pan.view];
NSLog(@"拖拽事件 %@", NSStringFromCGPoint(point));
CGPoint temp = self.iconView.center;
temp.x += point.x;
temp.y += point.y;
self.iconView.center = temp;
// 理解不了就记住就OK
[pan setTranslation:CGPointZero inView:pan.view];
}
旋转:
- (void)rotationTest
{
// 旋转
UIRotationGestureRecognizer *gesture = [[UIRotationGestureRecognizer alloc] init];
gesture.delegate = self;
[self.iconView addGestureRecognizer:gesture];
[gesture addTarget:self action:@selector(rotationView:)];
}
- (void)rotationView:(UIRotationGestureRecognizer *)gesture
{
// NSLog(@"旋转事件 %.1f", gesture.rotation);
// 每次从最初的位置开始
// self.iconView.transform = CGAffineTransformMakeRotation(gesture.rotation);
// 在传入的transform基础上递增一个弧度
self.iconView.transform = CGAffineTransformRotate(self.iconView.transform, gesture.rotation);
// 将旋转的弧度清零(注意不是将图片旋转的弧度清零, 而是将当前手指旋转的弧度清零)
gesture.rotation = 0;// 如果理解不了 , 记住就OK
}
捏合
- (void)pichTest
{
// 捏合手势
UIPinchGestureRecognizer *pinch = [[UIPinchGestureRecognizer alloc] init];
pinch.delegate = self;
[self.iconView addGestureRecognizer:pinch];
[pinch addTarget:self action:@selector(pinchView:)];
}
- (void)pinchView:(UIPinchGestureRecognizer *)pinch
{
// NSLog(@"捏合事件 %.1f", pinch.scale);
// self.iconView.transform = CGAffineTransformMakeScale(pinch.scale, pinch.scale);
// 1.0 * 0.9
self.iconView.transform = CGAffineTransformScale(self.iconView.transform, pinch.scale, pinch.scale);
pinch.scale = 1.0;
}
同时旋转和缩放
这样只能用一个手势,然后为了能让他能同时旋转和缩放
增加代理,让能判断多个手势。
要实现代理
@interface NYViewController ()<UIGestureRecognizerDelegate>
// 该方法返回的BOOL值决定了view是否能够同时响应多个手势
- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldRecognizeSimultaneouslyWithGestureRecognizer:(UIGestureRecognizer *)otherGestureRecognizer
{
NSLog(@"%@ - %@", gestureRecognizer.class, otherGestureRecognizer.class);
return YES;
}
注意:
然后呢还有个要注意的地方
UIImageView要勾中这两个才可以能实现交互
中间的一些算法什么的就不多说了,前面的博客里面有画的图片,大家感兴趣的可以看看。反正猫猫一般都直接拿来用,或者凭借感觉猜(这个不建议大家学习。。。)
时间: 2024-10-05 06:38:13