作者热门文章
- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
这是 UIPanGesture 子类:
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
NSLog(@"touches began");
self ->origLoc = [[touches anyObject] locationInView:self.view.superview];
[super touchesBegan:touches withEvent:event];
}
- (void) touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event {
if (self.state == UIGestureRecognizerStatePossible) {
CGPoint loc = [[touches anyObject] locationInView:self.view.superview];
CGFloat deltaX = fabsf(loc.x - origLoc.x);
CGFloat deltaY = fabsf(loc.y - origLoc.y);
if (deltaY >= deltaX) {
self.state = UIGestureRecognizerStateFailed;
} else {
[super touchesMoved:touches withEvent:event];
}
}
}
- (CGPoint) translationInView:(UIView *)v {
CGPoint proposedTranslation = [super translationInView:v];
proposedTranslation.y = 0;
return proposedTranslation;
}
这是我的子类 UIPanGesture 调用的方法:
- (void)dragging: (UIPanGestureRecognizer *)p {
UIView *vv = p.view;
if (p.state == UIGestureRecognizerStateBegan || p.state == UIGestureRecognizerStateChanged) {
CGPoint delta = [p translationInView:vv.superview];
CGPoint c = vv.center;
c.x += delta.x;
c.y += delta.y;
vv.center = c;
[p setTranslation:CGPointZero inView:vv.superview];
}
}
当我尝试拖动 View 时,它只移动了很小的量,然后就停止了。我需要不断地轻推它才能让它移动。有什么想法吗?
提前致谢,
迈克尔。
最佳答案
在我之前的评论中,我不清楚您为什么要子类化,但您澄清了,这是因为您有多个手势处理程序正在进行。非常好。
这里是自定义平移手势 HorizontalPanGestureRecognizer 的一些示例代码,仅当第一次移动是水平时才会触发:
// HorizontalPanGestureRecognizer.h
#import <UIKit/UIKit.h>
@interface HorizontalPanGestureRecognizer : UIPanGestureRecognizer
@end
还有
// HorizontalPanGestureRecognizer.m
#import "HorizontalPanGestureRecognizer.h"
#import <UIKit/UIGestureRecognizerSubclass.h>
@interface HorizontalPanGestureRecognizer ()
{
BOOL _hasConfirmedDirection;
BOOL _wasHorizontal;
CGPoint _origLoc;
}
@end
@implementation HorizontalPanGestureRecognizer
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
[super touchesBegan:touches withEvent:event];
UITouch *touch = [touches anyObject];
_hasConfirmedDirection = NO;
_origLoc = [touch locationInView:self.view];
}
- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event
{
[super touchesMoved:touches withEvent:event];
if (self.state == UIGestureRecognizerStateFailed) return;
if (!_hasConfirmedDirection)
{
CGPoint translation = [self translationInView:self.view];
_hasConfirmedDirection = YES;
_wasHorizontal = (fabs(translation.x) > fabs(translation.y));
}
if (!_wasHorizontal)
self.state = UIGestureRecognizerStateFailed;
}
- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event
{
[super touchesEnded:touches withEvent:event];
if (!_wasHorizontal)
self.state = UIGestureRecognizerStateFailed;
}
- (void)touchesCancelled:(NSSet *)touches withEvent:(UIEvent *)event
{
[super touchesCancelled:touches withEvent:event];
if (!_wasHorizontal)
self.state = UIGestureRecognizerStateFailed;
}
- (CGPoint)locationInView:(UIView *)view
{
CGPoint superLocation = [super locationInView:view];
if (_hasConfirmedDirection)
superLocation.y = _origLoc.y;
return superLocation;
}
- (CGPoint)translationInView:(UIView *)view
{
CGPoint superTranslation = [super translationInView:view];
if (_hasConfirmedDirection)
superTranslation.y = 0.0f;
return superTranslation;
}
@end
然后您可以让主视图 Controller 中的处理程序适本地使用它(在本示例中,只需拖动 UILabel 即可)。在viewDidLoad中,创建手势:
HorizontalPanGestureRecognizer *recognizer = [[HorizontalPanGestureRecognizer alloc] initWithTarget:self action:@selector(handleCustomPan:)];
[self.view addGestureRecognizer:recognizer];
然后处理程序将类似于:
- (void)handleCustomPan:(UIPanGestureRecognizer *)sender
{
switch (sender.state) {
case UIGestureRecognizerStateChanged:
if (!_panLabel)
{
// In my case I'm creating a UILabel to drag around, whereas you might just drag
// whatever countrol you want to drag.
//
// But, regardless, I'm keeping track of the original center in _panLabelOrigCenter
[self makePanLabel:sender];
}
CGPoint translate = [sender translationInView:self.view];
_panLabel.center = CGPointMake(_panLabelOrigCenter.x + translate.x, _panLabelOrigCenter.y + translate.y);
break;
case UIGestureRecognizerStateEnded:
[self removePanLabel];
break;
default:
break;
}
}
(显然,这是 ARC 代码。如果是非 ARC,请添加必要的附加代码行以进行常规内存管理。)
关于ios - 为什么我的平移手势只会轻推其 View 并停止?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11009154/
我正在尝试找到一种微调 WPF 窗口的方法(类似于 msn messenger 窗口用于微调的效果)。我知道有些人会说我不应该这样做,但这是为了满足特定的用户需求。我所说的轻推是指将窗口位置摇动大约半
我是一名优秀的程序员,十分优秀!