【问题标题】:Zoom with touches in a custom view in a scrollView. touchesMoved not called在滚动视图中的自定义视图中使用触摸进行缩放。 touchesMoved 未调用
【发布时间】:2014-02-19 03:58:34
【问题描述】:

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

scrollView 有一个子视图,用户可以触摸和移动它。它将像一张小地图一样运作。 subView 上有几个自定义标签。我在标签上有一个点击手势识别器,现在可以正常工作,并且在校正缩放和移动后仍然需要工作。

我还尝试了[scrollView setScrollEnabled:NO]; 和其他东西,但这并不能让两者都起作用。如何调用已移动?我需要做什么才能让两者都起作用?

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

【问题讨论】:

    标签: ios objective-c uiview uiscrollview uitapgesturerecognizer


    【解决方案1】:

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

    我将此添加到我的子视图中:

    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:] 的调用的消息,但这似乎有效。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-11-03
      • 1970-01-01
      • 2023-03-08
      相关资源
      最近更新 更多