背景图像也滚动而滚动的UIScrollView内容(background image also sc

2019-10-17 02:07发布

我怎么可以设置背景图片一直固定的位置,同时我滚动我滚动内容content.Now意味着我的背景图片也保持movingbackground图像也滚动而在UISCROLLVIEW..My示例代码滚动内容下面贴。

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

}

Answer 1:

它的滚动,因为它是滚动视图的背景。 所以,当滚动动作,背景移动。 你可以做滚动视图的背景是透明的(可能是背景色设置为[UIColor clearColor]和设置opaqueNO ),然后把具有相同的帧滚动视图后视图。 这不会滚动视图中移动。 请记住,滚动视图中移动自己的内容,而不是自己。 背景是内容的一部分。

编辑:

更改此:

- (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];


文章来源: background image also scroll while scroll content in UISCROLLVIEW
标签: ios xcode macos