iOS UIView 中的 UIScrollView

发布于 2024-11-01 06:08:39 字数 1446 浏览 0 评论 0原文

我有一个具体的问题。对于大多数人来说这可能并不重要,但我必须处理它并且我必须解决下面描述的问题。我尝试使用 Google 和 Apple SDK 文档查找有关它的一些信息,但没有成功。

我正在设计一个屏幕,其中有许多水平滚动的图像。共有三三卷相同的卷轴。每卷都有标题。我已经实现了从 UIView 派生的自定义类,并将 UIScrollView 用于滚动,将 UILabel 用于标题文本:

@interface MyView : UIView {
    UIScrollView *iScrollView;
    UIView       *iTitleView;
}

然后我将此类的对象放在UIViewController 的视图:

@implementation MyViewController

- (void) viewDidLoad {
    [super viewDidLoad];
    ...
    iScrollViewTop = [[MyView alloc] init];
    [self.view addSubview:iScrollViewTop];
    ...
}

@end

当我用图像填充内部滚动视图并运行我的应用程序时,它看起来不错。但有一些奇怪的行为。首先,滚动没有像我设置的那样反弹

iScrollView.bounces = NO;

,其次,当我滑动滚动时,滚动停止后,滚动条不会在一秒钟内消失。这对我来说很奇怪,因为当我通常创建一个 UIScrollView 并将其添加到 UIViewController 的视图中时,它会反弹,并且滚动条在停止时会立即消失。我尝试更改 UIScrollView 的属性,例如 orientationLockEnabledpagingEnabledcanCancelContentTouchesdelaysContentTouches< /code>、decelerationRate 等。事实上,我尝试更改 UIScrollView 的几乎所有属性,但我无法让滚动条立即消失。

如果我尝试将 UIScrollView 而不是 MyView 添加到 UIViewController.view 中,它会弹起,滚动条在停止后立即消失。另外,如果我从 UIScrollView 子类 MyView ,我也会得到正确的行为,但在这种情况下,我无法管理标题标签,因为它与其他内容一起滚动。

所以这是我的问题:

  1. 你知道为什么我会看到这种行为吗?
  2. 如何获得 UIView 封装的滚动的“通常”行为?

I have a little bit specific question. It might not matter for most people but I have had to deal with it and I had to solve the issue described below. I tried to find some information about it using Google and the Apple SDK documentation but did not succeed.

I was a designing a screen where there were many images in horizontal scrolls. There three three same scrolls. Every scroll had title. I have implemented custom class derived from UIView and placed there UIScrollView for scroll and UILabel for title text:

@interface MyView : UIView {
    UIScrollView *iScrollView;
    UIView       *iTitleView;
}

I then put objects of this class on the view of a UIViewController:

@implementation MyViewController

- (void) viewDidLoad {
    [super viewDidLoad];
    ...
    iScrollViewTop = [[MyView alloc] init];
    [self.view addSubview:iScrollViewTop];
    ...
}

@end

When I filled the internal scroll view with images and ran my application it looked OK. But there was some strange behavior. First, scroll did not have bounces as if I had set

iScrollView.bounces = NO;

and second, when I swiped to scroll, after the scroll stopped, the scroll bar did not disappear within one second. It was strange for me, because when I usually create a UIScrollView and add it to the UIViewController's view it has bounces and scroll bar disappears immediately when it stops. I tried to change UIScrollView's properties, such as directionalLockEnabled, pagingEnabled, canCancelContentTouches, delaysContentTouches, decelerationRate and others. In fact, I have tried to change almost all properties of UIScrollView but I could not get the scroll bars to immediately disappear.

If I try to add UIScrollView instead MyView to the UIViewController.view, it bounces and scroll bar disappears immediately after it stops. Also I get correct behavior if I subclass MyView from UIScrollView but in this case I cannot manage the title label because it scrolls together with other content.

So here are my questions:

  1. Do you know why I am seeing this behavior?
  2. How can I get "usual" behavior for scroll encapsulated by UIView?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

似梦非梦 2024-11-08 06:08:39

遵循此模式(westie.jpg = 360x200 的图像)

@interface MyView : UIView
{
UIScrollView *sv;
UILabel *l;
}
-(MyView*)initWithFrame:(CGRect)frame;
@end

@implementation MyView
-(MyView*)initWithFrame:(CGRect)frame;
{
self = [super initWithFrame:frame];
sv = [[UIScrollView alloc] initWithFrame:CGRectMake(0,0,360,200)];
sv.scrollEnabled = YES;
sv.contentSize = CGSizeMake(360*3,200);
[self addSubview:sv];

UIImage *i1 = [UIImage imageNamed:@"westie.jpg"];
UIImageView *iv1 = [[UIImageView alloc] initWithImage:i1];
iv1.frame = CGRectMake(360*0, 0, 360, 200);
[sv addSubview:iv1];
UIImageView *iv2 = [[UIImageView alloc] initWithImage:i1];
iv2.frame = CGRectMake(360*1, 0, 360, 200);
[sv addSubview:iv2];
UIImageView *iv3 = [[UIImageView alloc] initWithImage:i1];
iv3.frame = CGRectMake(360*2, 0, 360, 200);
[sv addSubview:iv3];

l = [[UILabel alloc] initWithFrame:CGRectMake(10, 10, 100, 20)];
l.text = @"Hello World";
l.backgroundColor = [UIColor blueColor];
[self addSubview:l];

return self;
}

@end :

好的,下面是 hacky 代码,所以忽略我的所有其他问题,但稍后在外部视图创建中

[window addSubview:[[MyView alloc] initWithFrame:CGRectMake(0, 20, 360, 200)]];

ok, hacky code follows, so ignore all my other issues, but follow this pattern (westie.jpg = image that was 360x200)

@interface MyView : UIView
{
UIScrollView *sv;
UILabel *l;
}
-(MyView*)initWithFrame:(CGRect)frame;
@end

@implementation MyView
-(MyView*)initWithFrame:(CGRect)frame;
{
self = [super initWithFrame:frame];
sv = [[UIScrollView alloc] initWithFrame:CGRectMake(0,0,360,200)];
sv.scrollEnabled = YES;
sv.contentSize = CGSizeMake(360*3,200);
[self addSubview:sv];

UIImage *i1 = [UIImage imageNamed:@"westie.jpg"];
UIImageView *iv1 = [[UIImageView alloc] initWithImage:i1];
iv1.frame = CGRectMake(360*0, 0, 360, 200);
[sv addSubview:iv1];
UIImageView *iv2 = [[UIImageView alloc] initWithImage:i1];
iv2.frame = CGRectMake(360*1, 0, 360, 200);
[sv addSubview:iv2];
UIImageView *iv3 = [[UIImageView alloc] initWithImage:i1];
iv3.frame = CGRectMake(360*2, 0, 360, 200);
[sv addSubview:iv3];

l = [[UILabel alloc] initWithFrame:CGRectMake(10, 10, 100, 20)];
l.text = @"Hello World";
l.backgroundColor = [UIColor blueColor];
[self addSubview:l];

return self;
}

@end

later, in your outer view creation:

[window addSubview:[[MyView alloc] initWithFrame:CGRectMake(0, 20, 360, 200)]];
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文