2014-02-06 92 views
1

我是Xcode 5和Objective C的新手。我想開發文字處理應用程序。不過,我不知道如何用Xcode來完成它。如何在ScrollView中添加標籤

如何在ScrollView內左右滑動方向添加標籤:左邊預覽,右邊是下一個單詞?

如何在Xcode中添加100個單詞到UIlabel

例如,向右滑動顯示下一個單詞「Apple」,然後再向右滑動顯示下一個單詞「Bananas」;向左滑動即可預覽:「Apple」。

+0

請補充說明你已經嘗試的代碼。 – wruckie

+0

下面是我嘗試過的代碼。 – user3280722

回答

0

這是UIScrollView分頁的行爲。在這裏你可以找到documentation

如何將UILabel(通常是UIView中的任何實例)添加到UIScrollView。

UIScrollView* scrollView = [[UIScrollView alloc] initWithFrame:self.view.bounds]; 
scrollView.pagingEnabled = YES; // replace default scroll with paging 
for (int i = 0; i < mWordsCount; i++) 
{ 
    UILabel* textLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width * i, self.view.frame.size.height)]; 
    textLabel.text = @"currnet word"; // wordsArray[i]; or [wordsArray objectAtIndex:i]; 

    [scrollView addSubview:textLabel]; 
    // Uncomment if you don't use ARC 
    //[textLabel release]; 
} 
0

//scrollVC.h

#import <UIKit/UIKit.h> 

@interface scrollVC : UIViewController 
@property (weak, nonatomic) IBOutlet UIScrollView *mainScroll; 

@end 

//scrollVC.m

#import "scrollVC.h" 

@interface scrollVC() 

@end 

@implementation scrollVC 

- (void)viewDidLoad { 
    [super viewDidLoad]; 
    // Do any additional setup after loading the view. 


    [self.mainScroll setContentSize:CGSizeMake(960, 995)]; 
    [self.mainScroll setPagingEnabled:YES]; 

    UILabel *testLabel1 = [[UILabel alloc]initWithFrame:CGRectMake(350, 100, 100, 44)]; 
    [testLabel1 setText:@"testLabel1"]; 
    [testLabel1 setBackgroundColor:[UIColor redColor]]; 
    [self.mainScroll addSubview:testLabel1]; 

    UILabel *testLabel2 = [[UILabel alloc]initWithFrame:CGRectMake(660, 100, 100, 44)]; 
    [testLabel2 setText:@"testLabel2"]; 
    [testLabel2 setBackgroundColor:[UIColor redColor]]; 
    [self.mainScroll addSubview:testLabel2]; 
} 

- (void)didReceiveMemoryWarning { 
    [super didReceiveMemoryWarning]; 
    // Dispose of any resources that can be recreated. 
} 

/* 
#pragma mark - Navigation 

// In a storyboard-based application, you will often want to do a little preparation before navigation 
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender { 
    // Get the new view controller using [segue destinationViewController]. 
    // Pass the selected object to the new view controller. 
} 
*/ 

@end