我正在尝试使用UIPageControl创建一个UIScrollView,页面包含各种宽度的标签。 问题是我不知道如何设置每个页面的宽度。
为了让自己更好地理解,这是我的代码:
:
#import <UIKit/UIKit.h>
@interface ViewController : UIViewController<UIScrollViewDelegate>
- (void)pageChangedInSecondScroll;
@end
并在.m文件中:
#import "ViewController.h"
@interface ViewController (){
//second scroll
UIPageControl *secondPageControl;
UIScrollView *secondScrollView;
//labels
UILabel *publicLabel;
UILabel *recentLabel;
UILabel *favoritesLabel;
UILabel *joinedLabel;
UILabel *myChannelsLabel;
UILabel *suggestedLabel;
//Labels X positons
int curentLabelX;
int initialPublicLabelX;
int initialRecentLabelX;
int initialFavoritesX;
int initialJoinedX;
int initialChannelsLabelX;
int initialSuggestedLabelX;
//Labels aspect ratio
int publicLabelAspectRatio;
int recentLabelAspectRatio;
int favoritesAspectRatio;
int joinedAspectRatio;
int channelsLabelAspectRatio;
int suggestedLabelAspectRatio;
}
@end
@implementation ViewController
- (void)viewDidLoad
{
[super viewDidLoad];
self.view.backgroundColor=[UIColor clearColor];
//Scroll view for labels
secondScrollView = [[UIScrollView alloc]initWithFrame:CGRectMake(0, 80.f, [[UIScreen mainScreen] bounds].size.width,23.f)];
[secondScrollView setShowsHorizontalScrollIndicator:NO];
secondScrollView.backgroundColor=[UIColor redColor];
secondScrollView.delegate=self;
secondScrollView.pagingEnabled=YES;
[secondScrollView setContentSize:CGSizeMake(509.f, secondScrollView.frame.size.height)];
// page control
secondPageControl = [[UIPageControl alloc]initWithFrame:CGRectMake(0, 0, 320, 23.f)];
secondPageControl.backgroundColor=[UIColor blackColor];
secondPageControl.numberOfPages=6;
[secondPageControl addTarget:self action:@selector(pageChangedInSecondScroll) forControlEvents:UIControlEventValueChanged];
secondPageControl.alpha = 1;
[self.view addSubview:secondScrollView];
[self.view addSubview:secondPageControl];
//public label
publicLabel = [[UILabel alloc] initWithFrame:CGRectMake(0.f, 0.f, 59.f, 23.f)];
initialPublicLabelX = 0;
publicLabelAspectRatio = [[UIScreen mainScreen] bounds].size.width / 64.f;
publicLabel.backgroundColor = [UIColor redColor];
publicLabel.text = @"public";
[publicLabel setFont:[UIFont fontWithName:@"Helvetica-Bold" size:20]];
publicLabel.textColor = [UIColor whiteColor];
[secondScrollView addSubview:publicLabel];
//recent label
recentLabel = [[UILabel alloc] initWithFrame:CGRectMake(64.f, 0.f, 60.5f, 23.f)];
initialRecentLabelX = 64.f;
recentLabelAspectRatio = [[UIScreen mainScreen] bounds].size.width / 64.f;
NSLog(@"%d", recentLabelAspectRatio);
recentLabel.backgroundColor = [UIColor redColor];
recentLabel.text = @"recent";
[recentLabel setFont:[UIFont fontWithName:@"Helvetica-Bold" size:20]];
recentLabel.textColor = [UIColor whiteColor];
[secondScrollView addSubview:recentLabel];
//favorites label
favoritesLabel = [[UILabel alloc] initWithFrame:CGRectMake(129.5f, 0.f, 83.5f, 23.f)];
initialFavoritesX = 129.5f;
favoritesAspectRatio = [[UIScreen mainScreen] bounds].size.width / 65.5f;
favoritesLabel.backgroundColor = [UIColor redColor];
favoritesLabel.text = @"favorites";
[favoritesLabel setFont:[UIFont fontWithName:@"Helvetica-Bold" size:20]];
favoritesLabel.textColor = [UIColor whiteColor];
[secondScrollView addSubview:favoritesLabel];
//joined label
joinedLabel = [[UILabel alloc] initWithFrame:CGRectMake(218.f, 0.f, 59.f, 23.f)];
initialJoinedX = 218.f;
joinedAspectRatio = [[UIScreen mainScreen] bounds].size.width / 88.5f;
joinedLabel.backgroundColor = [UIColor redColor];
joinedLabel.text = @"joined";
[joinedLabel setFont:[UIFont fontWithName:@"Helvetica-Bold" size:20]];
joinedLabel.textColor = [UIColor whiteColor];
[secondScrollView addSubview:joinedLabel];
//my channels label
myChannelsLabel = [[UILabel alloc] initWithFrame:CGRectMake(282.f, 0.f, 121.5f, 23.f)];
initialChannelsLabelX = 282.f;
channelsLabelAspectRatio = [[UIScreen mainScreen] bounds].size.width / 64.f;
myChannelsLabel.backgroundColor = [UIColor redColor];
myChannelsLabel.text = @"my channels";
[myChannelsLabel setFont:[UIFont fontWithName:@"Helvetica-Bold" size:20]];
myChannelsLabel.textColor = [UIColor whiteColor];
[secondScrollView addSubview:myChannelsLabel];
//suggested label
suggestedLabel = [[UILabel alloc] initWithFrame:CGRectMake(408.5f, 0.f, 100.5f, 23.f)];
initialSuggestedLabelX = 418.5f;
suggestedLabelAspectRatio = [[UIScreen mainScreen] bounds].size.width / 126.5;
suggestedLabel.backgroundColor = [UIColor redColor];
suggestedLabel.text = @"suggested";
[suggestedLabel setFont:[UIFont fontWithName:@"Helvetica-Bold" size:20]];
suggestedLabel.textColor = [UIColor whiteColor];
[secondScrollView addSubview:suggestedLabel];
}
- (void)scrollViewDidScroll:(UIScrollView *)_scrollView{
if (_scrollView == secondScrollView) {
NSLog(@"entered second scroll view delegate");
if (_scrollView.contentOffset.x < 30.f) {
secondPageControl.currentPage=0;
}else if (_scrollView.contentOffset.x >= 30.f && _scrollView.contentOffset.x < 94.f){
secondPageControl.currentPage=1;
}else if (_scrollView.contentOffset.x >= 94.f && _scrollView.contentOffset.x < 172.f){
secondPageControl.currentPage=2;
}else if (_scrollView.contentOffset.x >= 172.f && _scrollView.contentOffset.x < 240.f){
secondPageControl.currentPage=3;
}else if (_scrollView.contentOffset.x >= 240.f && _scrollView.contentOffset.x < 172.f){
secondPageControl.currentPage=4;
}else if (_scrollView.contentOffset.x >= 344.f && _scrollView.contentOffset.x < 509.f){
secondPageControl.currentPage=5;
}
}
}
- (void)pageChangedInSecondScroll{
int pageNumber = (int)secondPageControl.currentPage;
NSLog(@"Page number received is: %d",pageNumber);
CGRect frame = secondScrollView.frame;
if (pageNumber == 1) {
frame.origin.x = 0;
}else if (pageNumber == 2){
frame.origin.x = 64.f;
}else if (pageNumber == 3){
frame.origin.x = 129.5f;
}else if (pageNumber == 4){
frame.origin.x = 218.f;
}else if (pageNumber == 5){
frame.origin.x = 282.f;
}else if (pageNumber == 6){
frame.origin.x = 408.5f;
}
frame.origin.y=0;
[secondScrollView scrollRectToVisible:frame animated:YES];
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
- (void)viewDidUnload {
[super viewDidUnload];
}
@end
投票给所有答案!