2012-12-04 16 views
0

我有一個視圖控制器和一個UIView現在在我的ViewController當我點擊barButton比第二個視圖將Apear像彈出,現在我想添加頁面控制器和滾動視圖在第二個看來,我的第二個觀點是不UIViewController的,但它是UIView.so我怎麼能做到這一點...想在UIView編程實現UIPageController和UIScrollView在ios

我的第一個觀點是「ImageViewController」和第二種觀點是「PopupView」在ImageViewController.m

#import "PopupView.h" 

@implementation ImageViewController 

- (void)viewDidLoad 
{ 
UIBarButtonItem *clipArt = [[UIBarButtonItem alloc] initWithTitle:@"Clip Art" 
                   style:UIBarButtonItemStyleBordered 
                   target:self 
                   action:@selector(popUpView:)]; 
} 

- (void)popUpView:(id)sender { 
    CGRect * imageFrame = CGRectMake(10, 90, 300, 300); 
    PopupView *popUpView = [[PopupView alloc] initWithFrame:imageFrame]; 
    [self.view addSubview:popUpView]; 

} 

而在PopupView.m

- (id)initWithFrame:(CGRect)frame 
{ 


    if (self) { 
     CGRect * imageFrame = CGRectMake(0, 0, 300, 300); 
     self = [super initWithFrame:frame]; 
     UIImageView *starImgView = [[UIImageView alloc] initWithFrame:imageFrame]; //create ImageView 

     starImgView.alpha =0.8; 
     starImgView.layer.cornerRadius = 15; 
     starImgView.layer.masksToBounds = YES; 
     starImgView.image = [UIImage imageNamed:@"black"]; 

     [self addSubview:starImgView]; 
     self.backgroundColor = [UIColor clearColor]; 
} 
    return self; 
} 
+0

請***不要濫用Xcode標籤*** – 2012-12-04 14:42:17

+0

什麼標籤,我沒有做過任何... –

+0

你用這個問題的'xcode'標籤,而它與Xcode無關。今後請避免這種情況。 – 2012-12-04 14:51:27

回答

0

在第二視圖實現這樣的,對於網頁控制滾動視圖(這個例子說明了兩種觀點的情況下):

CGRect scrollViewFrame = CGRectMake(ur dimensions for scroll view); 
UIScrollView *myScrollView = [[UIScrollView alloc] initWithFrame:scrollViewFrame]; 
myScrollView.pagingEnabled = YES; 
myScrollView.contentSize = CGSizeMake(scrollViewFrame.size.width * 2, scrollViewFrame.size.height); 
//content size is the actual size of the content to be displayed in the scroll view. 
//Since, here we want to add two views so multiplied the width by two. 

CGRect viewFrame = [myScrollView bounds]; 

UIView *view1 = [[UIView alloc] initWithFrame:viewFrame]; 

viewFrame.origin.x = viewFrame.size.width; //setting the offset so that view2 is next to view 1 
UIView *view2 = [[UIView alloc] initWithFrame:viewFrame]; 

//add both the view to scroll view 
[myScrollView addSubview:view1]; 
[myScrollView addSubview:view2]; 

//add scroll view to parent view 
[self addSubview:myScrollView]; 

,可隨時更換廠景/ 2與任何視覺元素。要使用頁面控件進行滾動,請確保您要添加到滾動視圖的所有視圖都是相同的寬度/高度。這種方式它開箱即用,你不必做任何事情。此外,將UIPageControl添加到視圖以向用戶提供某種反饋總是一個好主意。它的可選,但。

還有一點,如果您想要使用頁面控制進行水平滾動,請按照上述方法增加寬度。如果你想垂直滾動,增加高度並保持寬度相同。