在scrollView中以自定义视图中的触摸进行缩放。 touchesMoved未调用

时间:2014-02-19 03:58:34

标签: ios objective-c uiview uiscrollview uitapgesturerecognizer

我有一个自定义视图,可以捏缩放,但我也想在任何方向滑动它。缩放效果很好,移动效果很好,但到目前为止它只是其中之一。 touchesMoved方法永远不会被调用。

scrollView有一个用户将触摸和移动的子视图。它的功能就像一张小地图。在subView上有几个自定义标签。我在标签上有一个水龙头手势识别器,现在工作正常,并且在更正变焦和移动时仍然需要工作。

我也尝试了[scrollView setScrollEnabled:NO];等其他事情,但这并不能让两者都有效。怎么能触动Moved?我需要做些什么才能让两者都有效?

- (void)viewDidLoad
{
    [super viewDidLoad];

    [self.view setUserInteractionEnabled:YES];

    scrollView = [[UIScrollView alloc] initWithFrame:self.view.bounds];
    [scrollView setMinimumZoomScale:.7];
    [scrollView setMaximumZoomScale:1.5];
    [scrollView setAutoresizesSubviews:YES];
    [scrollView setUserInteractionEnabled:YES];
     scrollView.delegate = self;
    [self.view addSubview:scrollView];

    CGRect mapFrame = CGRectMake(0, 0, 300, 300);//temp numbers
    subView = [[UIView alloc]initWithFrame:mapFrame]; 
    subView.userInteractionEnabled=YES;
    subView.autoresizesSubviews=YES;  
    [scrollView addSubview:subView];
}


-(void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event {
        NSLog(@"touchesMoved");
    touch = [touches anyObject];
    CGPoint location = [touch locationInView:subView];
    CGPoint prevLoc = [touch previousLocationInView:subView];
    CGPoint origin = subView.frame.origin;
    origin.x += (location.x - prevLoc.x);
    origin.y += (location.y - prevLoc.y);
    CGRect newFrame = subView.frame;
    newFrame.origin = origin;
    subView.frame = newFrame;
}

- (UIView *)viewForZoomingInScrollView:(UIScrollView *)scrollView {
    return subView;
}

   //label sample
    newCity =[[MyCustomClass alloc] initWithFrame:CGRectMake(x, y, 95, 40)];
    newCity.userInteractionEnabled = YES;
    UITapGestureRecognizer *tapGesture = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(touchUp:)];
    [newCity addGestureRecognizer:tapGesture];
    [subView addSubview:newCity];

我也在日志中得到这个,不知道为什么。 “Ignoring call to [UIPanGestureRecognizer setTranslation:inView:] since gesture recognizer is not active. “我删除了平底锅手势识别器,之后无效。我正在使用Xcode 4.5.2

我的标题是

@interface CitiesViewController : UIViewController <UIScrollViewDelegate, UIGestureRecognizerDelegate>
{
    UIView      *subView;
}

@property(nonatomic, strong) UIView *subView;
@property(nonatomic, strong) UIScrollView *scrollView;

1 个答案:

答案 0 :(得分:0)

为了解决这个问题,我完全删除了TouchesMoved并使用了panGestureRecognizer。希望它可以帮助某人。

我将此添加到我的subView:

UIPanGestureRecognizer *pan = [[UIPanGestureRecognizer alloc]initWithTarget:self action:@selector(handlePan:)];
[pan setDelegate:self];
[subView addGestureRecognizer:pan];

添加此方法:

- (void)handlePan:(UIPanGestureRecognizer *)recognizer {

    CGPoint translation = [recognizer translationInView:subView];
    subView.center = CGPointMake(recognizer.view.center.x + translation.x,
                                 recognizer.view.center.y + translation.y);
}

我从未弄清楚为什么该消息忽略对[UIPanGestureRecognizer setTranslation:inView:]的调用仍在发生,但这似乎有效。