开发者

UIScrollView background image scrolls faster than content

i just noticed, having a backgroudn image inside of a UIScrollView along with some other UI-Elements, that they scroll with different speed.

i created a UIScrollView child in my main view, added this testing code:

-(void)viewDidLoad {
[super viewDidLoad];

oRulerYear.backgroundColor = [UIColor colorWithPatternImage: [UIImage imageNamed:@"bg.png"]];
//bigsize!
[oRulerYear开发者_JAVA技巧 setContentSize:CGSizeMake(2400,100)]; 

CGFloat x = 0;
CGFloat y = 0;

CGRect rect = CGRectMake(x , y, 120.0f, 45.0f);

//label 1
UILabel *label = [[[UILabel alloc] initWithFrame:rect] autorelease];
[label setText:@"Foo"];
[oRulerYear addSubview:label];


//label 2
x = 2000;
y = 10;
rect = CGRectMake(x , y, 120.0f, 45.0f);
UILabel *label2 = [[[UILabel alloc] initWithFrame:rect] autorelease];
[label2 setText:@"Bar"];
[oRulerYear addSubview:label2];

}

Scrolling DOES work, but i see a differen speed of scrolling on both label tags. so its quite like an 1980 pc game, or reverted parallax-scrolling or however its called. background seem to scroll faster than foreground.

Any ideas how to prevent that behaviour?


You need to set the background pattern on a child view of the paging UIScrollView, not on the paging UISCrollView itself.

#define OUTERPADDING 160
CGRect backgroundFrame = CGRectMake(-OUTERPADDING, 0,
     pagingScrollView.contentSize.width + 2*OUTERPADDING,
     pagingScrollView.contentSize.height);
UIView * backgroundView = [[UIView alloc] initWithFrame:backgroundFrame];
backgroundView.backgroundColor = [UIColor colorWithPatternImage:
        [UIImage imageNamed:@"walnut"]];
[pagingScrollView addSubview:backgroundView];

This view has to be as big as the content size and even more for a clean result. OUTERPADDING is used to ensure that the pattern is applied on half the screen before the first and after the last pages

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜