我希望改变一个现有的iOS应用程序,这样,与其使用多点触摸手势来调整和旋转图像(双指捏/缩放和扭转),我希望在图像的所有四个角落都有一个手柄,在顶部有一个手柄,这样用户就可以抓取其中一个手柄来调整大小或旋转。
我一直在研究这个话题,但是找不到任何指向正确方向的东西。
看看这张照片,看看我在说什么-

发布于 2014-02-05 16:23:15
我假设这是因为你开始的应用程序已经有工作的捏-缩放和扭转手势,你的问题只是如何显示那些半透明的圆圈为句柄。我倾向于创建绘制圆圈的UIView子类,如下所示:
@implementation HandleView
- (id)initWithFrame:(CGRect)frame
{
self = [super initWithFrame:frame];
if (self) {
self.backgroundColor = [UIColor clearColor];
self.userInteractionEnabled = YES;
}
return self;
}
- (void)drawRect:(CGRect)rect
{
CGContextRef context = UIGraphicsGetCurrentContext();
CGContextAddEllipseInRect(context, rect);
CGContextSetFillColorWithColor(context, [[UIColor colorWithWhite:1.0 alpha:0.5] CGColor]); // white translucent
CGContextSetStrokeColorWithColor(context, [[UIColor colorWithWhite:0.25 alpha:0.5] CGColor]); // dark gray translucent
CGContextSetLineWidth(context, 1.0);
CGContextDrawPath(context, kCGPathEOFillStroke); // draw both fill and stroke
}
@end如果您不想像我上面所做的那样使用Core调用编写自己的drawRect,那么您也可以通过drawRect层实现同样的效果。但想法是一样的。
然后,视图控制器可以添加这五个视图,并为它们添加手势识别器,如下所示:
HandleView *handleView = [[HandleView alloc] initWithFrame:CGRectMake(50, 50, 50, 50)];
[self.view addSubview:handleView];
UIPanGestureRecognizer *pan = [[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(handlePan:)];
[handleView addGestureRecognizer:pan];只需对屏幕上想要的每个句柄重复这一步骤即可。然后写你的手势识别器去做你想做的任何事情(例如移动包围矩形,移动适当的手柄等等)。
发布于 2014-02-05 17:04:01
听起来挺直接的。作为ASCII艺术的一个可能的解决方案的视图层次:
Container (ScalingRotatingView)
|
+----imageView (UIImageView)
|
+----upperLeftScalingHandle (HandleView)
|
+----upperRightScalingHandle (HandleView)
|
+----lowerLeftScalingHandle (HandleView)
|
+----lowerRightScalingHandle (HandleView)
|
+----rotatingHandle (HandleView)HandleView的所有实例都有一个pan手势识别器,它为控制器中的两种方法之一提供信息:
--updateForScaleGesture:,在适当更新所有视图的框架之前,使用手势识别器的-translationInView:来计算和存储新的缩放,并将转换重置为0;- -updateForRotationGesture:,在更新框架和重置识别器的转换之前,使用手势识别器的-translationInView:计算和存储新的角度。
对于这两种计算,都需要在图像视图的坐标系统中转换。对于缩放部分,您可以简单地将新的边缘长度除以自然图像维数,对于旋转,可以使用只有x分量才重要的近似:
sin(x) = x (对于x的小值)
哦,如果你的图像视图的锚点位于它的中心…,那肯定会有所帮助。
https://stackoverflow.com/questions/21581878
复制相似问题