开发者

iOS UIScrollView in 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 MyVie开发者_运维百科w 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?


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)]];
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜