滚动子视图左右,但没有向上或向下
问题描述:
我有一个观点 的子视图我希望用户是阿贝尔滚动这一观点的权利和只剩下。 但向上或向下滚动我想这个观点留在它的地方,我不希望它动的时候。 我该怎么做?滚动子视图左右,但没有向上或向下
我使用Objective C的适用于iOS的iPhone应用程序编码。
感谢
答
您可以使用UIScrollView
并设置contentSize
属性,使其height
相同视图的height
。
答
-
创建panRecognizer
UIPanGestureRecognizer *panRecognizer; panRecognizer = [[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(wasDragged:)]; [[self subview] addGestureRecognizer:panRecognizer];
2.创建wasDragged方法
- (void)wasDragged:(UIPanGestureRecognizer *)recognizer {
CGPoint translation = [recognizer translationInView:self.view];
CGRect recognizerFrame = recognizer.view.frame;
recognizerFrame.origin.x += translation.x;
// Check if UIImageView is completely inside its superView
if (CGRectContainsRect(self.view.bounds, recognizerFrame)) {
recognizer.view.frame = recognizerFrame;
}
// Else check if UIImageView is vertically and/or horizontally outside of its
// superView. If yes, then set UImageView's frame accordingly.
// This is required so that when user pans rapidly then it provides smooth translation.
else {
// Check vertically
if (recognizerFrame.origin.y < self.view.bounds.origin.y) {
recognizerFrame.origin.y = 0;
}
else if (recognizerFrame.origin.y + recognizerFrame.size.height > self.view.bounds.size.height) {
recognizerFrame.origin.y = self.view.bounds.size.height - recognizerFrame.size.height;
}
// Check horizantally
if (recognizerFrame.origin.x < self.view.bounds.origin.x) {
recognizerFrame.origin.x = 0;
}
else if (recognizerFrame.origin.x + recognizerFrame.size.width > self.view.bounds.size.width) {
recognizerFrame.origin.x = self.view.bounds.size.width - recognizerFrame.size.width;
}
}
// Reset translation so that on next pan recognition
// we get correct translation value
[recognizer setTranslation:CGPointZero inView:self.view];
}
你不明白我的意思,我想有一个可以滚动的小UI视图但只限于左侧和右侧。如果用户向下滚动,我想保留在顶栏之下,我不希望它从它的位置移动 –