在UISCROLLVIEW中滚动内容时,背景图像也会滚动 [英] background image also scroll while scroll content in UISCROLLVIEW

查看:56
本文介绍了在UISCROLLVIEW中滚动内容时,背景图像也会滚动的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在滚动内容时如何设置背景图像固定在同一位置.现在滚动内容意味着在UISCROLLVIEW中滚动内容时背景图像也保持移动背景图像也滚动.>

How can i set the background image keep fixed in same place while i scroll the content.Now i scroll content means my background images also keep movingbackground image also scroll while scroll content in UISCROLLVIEW..My sample code Posted Below.

My Code here

    - (void)viewDidLoad
{
    NSLog(@"Welcome to Home Page");
    [super viewDidLoad];

   // self.view.backgroundColor=[[UIColor alloc]initWithPatternImage:[UIImage imageNamed:@"bg-image.png"]];

    UIImage * img = [UIImage imageNamed:@"bg-image.png"];
    [scrollView setBackgroundColor:[UIColor colorWithPatternImage:img]];
// Do any additional setup after loading the view, typically from a nib.
}



- (void)loadView {


    CGRect fullScreenRect=[[UIScreen mainScreen] applicationFrame];
    scrollView=[[UIScrollView alloc] initWithFrame:fullScreenRect];
    scrollView.contentSize=CGSizeMake(1400, 100);

    UIImageView *tempImageView2 = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"sti01.png"]];
    tempImageView2.frame=CGRectMake(10, 60, 200, 200);
    [scrollView addSubview:tempImageView2];


    UIImageView *tempImageView3 = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"sti02.png"]];
    tempImageView3.frame=CGRectMake(240, 60, 200, 200);
    [scrollView addSubview:tempImageView3];

    UIImageView *tempImageView4 = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"sti03.png"]];
    tempImageView4.frame=CGRectMake(470, 60, 200, 200);
    [scrollView addSubview:tempImageView4];

    UIImageView *tempImageView5 = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"sti04.png"]];
    tempImageView5.frame=CGRectMake(700, 60, 200, 200);
    [scrollView addSubview:tempImageView5];


    UIImageView *tempImageView6 = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"sti05.png"]];
    tempImageView6.frame=CGRectMake(930, 60, 200, 200);
    [scrollView addSubview:tempImageView6];

    UIImageView *tempImageView7 = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"sti06.png"]];
    tempImageView7.frame=CGRectMake(1160, 60, 200, 200);
    [scrollView addSubview:tempImageView7];



    self.view=scrollView;
    [scrollView addSubview:tempImageView2];
    [scrollView addSubview:tempImageView3];
    [scrollView addSubview:tempImageView4];
    [scrollView addSubview:tempImageView5];
    [scrollView addSubview:tempImageView6];
    [scrollView addSubview:tempImageView7];

    scrollView.userInteractionEnabled = YES;
    btn = [UIButton buttonWithType:UIButtonTypeCustom];
    btn.frame = CGRectMake(22, 100, 1800, 500);
   // [btn setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
    [btn addTarget:self action:@selector(buttonTest:) forControlEvents:UIControlEventTouchUpInside];    
    [scrollView addSubview:btn];


}


- (IBAction)buttonTest:(id)sender {
    MSDescriptionpage *aSecondPageController = [[MSDescriptionpage  alloc] initWithNibName:@"MSDescriptionpage" bundle:nil];        
    [self.navigationController pushViewController:aSecondPageController animated:YES];        
    [aSecondPageController release];

}

推荐答案

正在滚动,因为它是滚动视图的背景.因此,当滚动移动时,背景也会移动.您可以使滚动视图的背景透明(可能是通过将背景色设置为 [UIColor clearColor] 并将 opaque 设置为 NO ),然后将具有相同框架的视图置于滚动视图的后面.这不会随滚动视图一起移动.请记住,滚动视图会移动其内容,而不是其自身.背景是该内容的一部分.

It's scrolling because it's the background of the scroll view. So when the scroll moves, the background moves. You could make the scroll view's background transparent (probably by setting the background color to [UIColor clearColor] and setting opaque to NO), and then putting a view behind the scroll view that has the same frame. This won't move with the scroll view. Remember that scroll views move their content, not themselves. The background is a part of that content.

更改此:

- (void)viewDidLoad
{
    NSLog(@"Welcome to Home Page");
    [super viewDidLoad];

    self.view.backgroundColor=[[UIColor alloc]initWithPatternImage:[UIImage imageNamed:@"bg-image.png"]];

    UIImage * img = [UIImage imageNamed:@"bg-image.png"];
    [scrollView setBackgroundColor:[UIColor colorWithPatternImage:img]];
 // Do any additional setup after loading the view, typically from a nib.
}

对此:

- (void)viewDidLoad
{
    NSLog(@"Welcome to Home Page");
    [super viewDidLoad];

    self.view.backgroundColor=[[UIColor alloc]initWithPatternImage:[UIImage imageNamed:@"bg-image.png"]];

    UIImage * img = [UIImage imageNamed:@"bg-image.png"];
    [scrollView setBackgroundColor:[UIColor clearColor]];
// Do any additional setup after loading the view, typically from a nib.
}

然后类似:

UIView *customScrollBackground = [[UIView alloc] initWithFrame:scrollView.frame];
customScrollBackground.backgroundColor = [[UIColor alloc] initWithPatternImage:[UIImage imageNamed:@"bg-image.png"]];
[customScrollBackground addSubview:scrollView];

这篇关于在UISCROLLVIEW中滚动内容时,背景图像也会滚动的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆