سؤال

I have a custom view that will pinch zoom, but I'd like to also slide it around in any direction. The zoom works well, and the moving works well, but so far it's just one or the other. The touchesMoved method never gets called.

The scrollView has a subView which the user will touch and move. It will function like a little map. On the subView are several custom labels. I have a tap Gesture recognizer on the labels, which works fine now, and will still need to work when the zoom and moving is corrected.

I also tried [scrollView setScrollEnabled:NO]; and other things, but that didn't allow both to work. How can touches Moved be called? What will I need to do to allow both to work?

- (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];

I'm also getting this in the log and don't know why. "Ignoring call to [UIPanGestureRecognizer setTranslation:inView:] since gesture recognizer is not active. " I removed the pan gesture recognizer, after that didn't work. I'm using Xcode 4.5.2

My header is

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

@property(nonatomic, strong) UIView *subView;
@property(nonatomic, strong) UIScrollView *scrollView;
هل كانت مفيدة؟

المحلول

To solve this I removed the TouchesMoved entirely and used a panGestureRecognizer instead. Hopefully it helps someone.

I added this to my subView:

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

Add this method:

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

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

I never did figure out why that message Ignoring call to [UIPanGestureRecognizer setTranslation:inView:] is still occurring, but this seems to work.

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top