프로그램/iOS

ScrollView를 이용하여 PageView처럼 사용

잡식성초보 2016. 3. 17. 16:23

스크롤뷰를 이용하여 PageView 처럼 사용하였다. 스크롤 뷰를 사용한 이유는 스크롤뷰에 bounce를 삭제하는 방법을 잘 모르고 찾아봣더니 어려운거 같아서 였다... 스크롤뷰의 바운스 제거는 쉽기 때문에 손이 많이 갔지만 스크롤뷰를 이용하였다. 혹시 이 글 보시고 pageView의 바운스 제거를 해보신분 있으시면 리플좀 부탁드립니다.



xib입니다.









h파일입니다.


m. 파일입니다. m파일은 밑에 소스 긁어 넣을테니 긁어가시길~ 주석도 달았습니다.




NSArray *imageArr = @[@"1",@"2",@"3",@"4"];

    

    CGFloat windowView = [UIScreen mainScreen].bounds.size.height//화면 높이

    CGFloat windowView2 = [UIScreen mainScreen].bounds.size.width//화면 넓이

    NSLog(@"width : %f     height: %f",windowView2, windowView);

    

    

    UIImageView *imgView1 = [[UIImageView alloc]init];

    UIImageView *imgView2 = [[UIImageView alloc]init];

    UIImageView *imgView3 = [[UIImageView alloc]init];

    UIImageView *imgView4 = [[UIImageView alloc]init];

    UIView *finalView = [[UIView alloc]init];

    UIButton *btn = [[UIButton alloc]init];

    


    [imgView1 setImage:[UIImage imageNamed:imageArr[0]]];

    [imgView2 setImage:[UIImage imageNamed:imageArr[1]]];

    [imgView3 setImage:[UIImage imageNamed:imageArr[2]]];

    [imgView4 setImage:[UIImage imageNamed:imageArr[3]]];

    

    

    CGFloat width = [UIScreen mainScreen].bounds.size.width;

    CGFloat height = [UIScreen mainScreen].bounds.size.height;

    

    

    self.scWidth.constant = width*5;    //페이지로 사용할 뷰의 넓이를 코드로 늘려줌. 화면이 5개라서 *5

    

    imgView1.frame = CGRectMake(0, 0, width, height);

    imgView2.frame = CGRectMake(width, 0, width, height);

    imgView3.frame = CGRectMake(width*2, 0, width, height);

    imgView4.frame = CGRectMake(width*3, 0, width, height);

    finalView.frame = CGRectMake(width*4, 0, width, height);

    btn.frame = CGRectMake(width/2-90, height/2-28, 180, 57);

    

    [imgView1 setBackgroundColor:[UIColor redColor]];

    [imgView2 setBackgroundColor:[UIColor blackColor]];

    [imgView3 setBackgroundColor:[UIColor blueColor]];

    [imgView4 setBackgroundColor:[UIColor brownColor]];

    [btn setBackgroundColor:[UIColor redColor]];

    [btn setTitle:@"Oh my god" forState:UIControlStateNormal];

    [btn addTarget:self action:@selector(bindData) forControlEvents:UIControlEventTouchUpInside];

    

    [finalView addSubview:btn];

    [self.scView addSubview:imgView1];

    [self.scView addSubview:imgView2];

    [self.scView addSubview:imgView3];

    [self.scView addSubview:imgView4];

    [self.scView addSubview:finalView];

    

    self.scrollView.bounces = NO;   //바운스 제거

    

    [self.scrollView setContentSize:CGSizeMake(width/4, 568)];  //페이지 처럼 사용하기 위한 사이즈잡아줌

    [self.scrollView setPagingEnabled:YES]; //페이지 처럼 사용하기


반응형

'프로그램 > iOS' 카테고리의 다른 글

Prefix 셋팅법  (0) 2016.04.30
iOS 라벨의 크기에 맞춰 동적으로 길이주기  (0) 2016.03.18
iOS second Data 변형  (0) 2016.03.15
AppDelegate 실행순서  (0) 2016.03.14
iOS 화면 이동하기.  (0) 2016.03.09