【发布时间】:2013-11-21 13:33:20
【问题描述】:
我如何编写自定义手势以在所有方向(即)原点侧和视图大小上增加视图的大小。
例子:
- 用户应该在任何一个角落触摸视图。靠近原点侧或查看结束侧。
示例:视图帧大小为(100,100,200,200).,最小触摸距离为距任意一侧 15 像素。
所以,如果用户触摸位置是(X=15,Y=40) in
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
方法是指靠近原点的一侧。
如果用户触摸位置是(X=15, Y=190) in,则相同
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
方法表示触摸靠近尺寸边。
我的代码:
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
[super touchesBegan:touches withEvent:event];
if([touches count] != self.touchMinimumCount ||
[[touches anyObject] tapCount] > self.tapMinimumCount)
{
self.state = UIGestureRecognizerStateFailed;
return;
}
self.startPoint = [[touches anyObject] locationInView:self.view];
self.viewFrame = self.view.frame;
if(((self.startPoint.x - self.view.bounds.origin.x) <= self.minimumTouchDistance) ||
((self.startPoint.y - self.view.bounds.origin.y) <= self.minimumTouchDistance) ||
((ABS(self.startPoint.x - self.view.bounds.size.width)) <= self.minimumTouchDistance) ||
((ABS(self.startPoint.y - self.view.bounds.size.height)) <= self.minimumTouchDistance))
{
self.state = UIGestureRecognizerStatePossible;
if(((self.startPoint.x - self.view.bounds.origin.x) <= self.minimumTouchDistance) ||
((self.startPoint.y - self.view.bounds.origin.y) <= self.minimumTouchDistance) )
{
self.currentTouchPosition = touchPositionIsOrigin;
}
else if(((ABS(self.startPoint.x - self.view.bounds.size.width)) <= self.minimumTouchDistance) ||
((ABS(self.startPoint.y - self.view.bounds.size.height)) <= self.minimumTouchDistance))
{
self.currentTouchPosition = touchPositionIsSize;
}
}
else
{
self.state = UIGestureRecognizerStateFailed;
}
}
如果触摸仅在触摸距离的一侧,我的手势可能状态设置。
现在我想根据用户手指移动调整视图框架的大小。
- 如果用户触摸原点侧并向左移动意味着想要改变视图的origin.x并且宽度也想要增加。
- 如果用户触摸原点侧并向顶部方向移动意味着,想要改变视图的origin.y,宽度也想要增加。
- 如果用户触摸原点侧并朝正确的方向移动意味着想要改变视图的origin.x。
- 如果用户触摸原点侧并向底部方向移动意味着要更改视图的origin.y。
- 如果用户触摸尺寸侧并向左移动意味着想要减小视图的宽度。
- 如果用户触摸尺寸侧并向顶部方向移动意味着要降低视图的高度。
- 如果用户触摸尺寸侧并向右移动意味着想要增加视图的宽度。
- 如果用户触摸尺寸侧并向底部方向移动意味着想要增加视图的高度。
这一切都会根据用户在该视图中移动时的手指而改变。
我的代码:
- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event
{
[super touchesMoved:touches withEvent:event];
if(self.state == UIGestureRecognizerStateFailed) return;
direction currentDirection;
CGPoint currentLocation = [[touches anyObject] locationInView:self.view];
if(self.state == UIGestureRecognizerStatePossible ||
self.state == UIGestureRecognizerStateChanged)
{
if((currentLocation.x - self.startPoint.x) > 0)
{
currentDirection = directionRight;
self.state = UIGestureRecognizerStateChanged;
if(self.currentTouchPosition == touchPositionIsOrigin)
{
distanceInOriginX = // need to implement.
distanceInSizeX = // need to implement.
}
else if(self.currentTouchPosition == touchPositionIsSize)
{
distanceInSizeX = // need to implement.
}
}
else if((currentLocation.x - self.startPoint.x) < 0)
{
currentDirection = directionLeft;
self.state = UIGestureRecognizerStateChanged;
if(self.currentTouchPosition == touchPositionIsOrigin)
{
distanceInOriginX = // need to implement.
distanceInSizeX = // need to implement.
}
else if(self.currentTouchPosition == touchPositionIsSize)
{
distanceInSizeX = // need to implement.
}
}
else if((currentLocation.y - self.startPoint.y) >= 0)
{
currentDirection = directionBottom;
self.state = UIGestureRecognizerStateChanged;
if(self.currentTouchPosition == touchPositionIsOrigin)
{
distanceInOriginY = // need to implement.
distanceInSizeY = // need to implement.
}
else if(self.currentTouchPosition == touchPositionIsSize)
{
distanceInSizeY = // need to implement.
}
}
else if((currentLocation.y - self.startPoint.y) < 0)
{
currentDirection = directionTop;
self.state = UIGestureRecognizerStateChanged;
if(self.currentTouchPosition == touchPositionIsOrigin)
{
distanceInOriginY = // need to implement.
distanceInSizeY = // need to implement.
}
else if(self.currentTouchPosition == touchPositionIsSize)
{
distanceInSizeY = // need to implement.
}
}
else
{
currentDirection = directionUnknown;
}
self.originPoint = CGPointMake(distanceInOriginX, distanceInOriginY);
self.sizePoint = CGPointMake(distanceInSizeX, distanceInSizeY);
}
}
我提到了 // 需要实现。我希望代码在哪里实现我在该点中提到的功能。所以,请帮助我们继续前进。
如果我的方法是错误的,请分享做相同类型手势的最佳方法。
有任何例子也请在这里分享。提前谢谢....
【问题讨论】:
标签: ios objective-c uigesturerecognizer