【发布时间】: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