2013-07-24 31 views
0

是否有可能在UIView中使用UIScrollView而不是UIViewController?是否可以在UIView中使用UIScrollView而不是在UIViewController中使用?

我試圖在一個UIView使用,但不起作用。我無法找到適合我的項目的樣本。

我需要在具有大小90x90像素的看法水平滾動。

這裏是我的代碼

UIScrollView* scrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 35, 400, 200)]; 
    [scrollView setScrollEnabled:YES]; 
    scrollView.pagingEnabled = YES; 
    [scrollView setDelegate:self]; 
    scrollView.backgroundColor = [UIColor brownColor]; 
    [scrollView setShowsHorizontalScrollIndicator:YES]; 
    [scrollView setIndicatorStyle:UIScrollViewIndicatorStyleDefault]; scrollView.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight; 
    scrollView.contentMode = UIViewContentModeScaleToFill; 
    [scrollView setContentSize:CGSizeMake(630,0)]; 

    [self addSubview:scrollView]; 


    //self.frame = CGRectMake(0, 0, 1024, 768); 

    [self setBackgroundColor:[[UIColor blackColor] colorWithAlphaComponent:0.8]]; 


    circleView = [[UIView alloc] initWithFrame:CGRectMake(100,100,90,90)];   
    circleView.backgroundColor = [[UIColor whiteColor] colorWithAlphaComponent:1]; 
    //circleView.layer.borderColor = [[UIColor blackColor] CGColor]; 
    //circleView.layer.borderWidth = 4; 

    UILabel* circleIndex = [[UILabel alloc] init]; 
    circleIndex.frame = CGRectMake(30, 25, 40, 40); 
    [circleIndex setFont:[UIFont fontWithName:@"Arial-BoldMT" size:40]]; 
    [circleIndex setText:@"A"]; 

    [circleView addSubview:circleIndex]; 


    circleView2 = [[UIView alloc] initWithFrame:CGRectMake(100+100,100,90,90)]; 
    circleView2.backgroundColor = [[UIColor whiteColor] colorWithAlphaComponent:1]; 

    circleView3 = [[UIView alloc] initWithFrame:CGRectMake(100+100+100,100,90,90)]; 
    circleView3.backgroundColor = [[UIColor whiteColor] colorWithAlphaComponent:1]; 

    circleView4 = [[UIView alloc] initWithFrame:CGRectMake(100+100+100+100,100,90,90)]; 
    circleView4.backgroundColor = [[UIColor whiteColor] colorWithAlphaComponent:1]; 

    circleView5 = [[UIView alloc] initWithFrame:CGRectMake(100+100+100+100+100,100,90,90)]; 
    circleView5.backgroundColor = [[UIColor whiteColor] colorWithAlphaComponent:1]; 

    circleView6 = [[UIView alloc] initWithFrame:CGRectMake(100+100+100+100+100+100,100,90,90)]; 
    circleView6.backgroundColor = [[UIColor whiteColor] colorWithAlphaComponent:1]; 

    [scrollView addSubview:circleView]; 

    [scrollView addSubview:circleView2]; 

    [scrollView addSubview:circleView3]; 

    [scrollView addSubview:circleView4]; 

    [scrollView addSubview:circleView5]; 

    [scrollView addSubview:circleView6]; 



    exitView = [[UIView alloc] initWithFrame:CGRectMake(70,-5,30,30)]; 
    exitView.layer.cornerRadius = 15; 

    exitView.backgroundColor = [UIColor redColor]; 
    exitView.layer.borderColor = [[UIColor whiteColor] CGColor]; 
    exitView.layer.borderWidth = 2; 

    [circleView addSubview:exitView]; 

    UIView *exitLabel = [[UIView alloc] initWithFrame:CGRectMake(8,13,15,3)]; 
    exitLabel.backgroundColor = [UIColor clearColor]; 
    exitLabel.layer.borderColor = [[UIColor whiteColor] CGColor]; 
    exitLabel.layer.borderWidth = 2; 


     [exitView addSubview:exitLabel]; 

    UILongPressGestureRecognizer *longPress = 
    [[UILongPressGestureRecognizer alloc] initWithTarget:self 
                action:@selector(handleLongPress:)]; 
    [circleView addGestureRecognizer:longPress]; 

    UITapGestureRecognizer *singlePress = 
    [[UITapGestureRecognizer alloc] initWithTarget:self 
                action:@selector(handleSinglePress:)]; 
    [exitView addGestureRecognizer:singlePress]; 
    //[longPress release]; 


    UITapGestureRecognizer *singlePress2 = 
    [[UITapGestureRecognizer alloc] initWithTarget:self 
              action:@selector(singlePress:)]; 
    [circleView2 addGestureRecognizer:singlePress2]; 



    [exitView setHidden:YES]; 

    [circleView bringSubviewToFront:exitView]; 




    [exitView setUserInteractionEnabled:YES]; 


} 
return self; 
} 
+0

是的,它是可能的。請分享您的代碼。 –

+0

請分享您的代碼。 – madLokesh

+0

'UIScrollView'是'UIView'的子類。任何UIView實例都可以是任何其他UIView的子視圖。 – fzwo

回答

0

我粘貼你的代碼,因爲它是我能看到滾動爲好。
您似乎將自己的代碼粘貼到自定義視圖的
- (id)initWithFrame:(CGRect)frame

你給你的觀點是什麼框架?

-1

我認爲這個問題是內容大小沒有定義正確

[scrollView setContentSize:CGSizeMake(630,0)]; 

給出了寬度630和0的高度意味着你試圖實現一個水平滾動。嘗試一些較大的內容大小,並嘗試滾動代碼看起來不錯

我嘗試了滾動視圖的uview,它運行良好

@implementation TestView 

- (id)initWithFrame:(CGRect)frame 
{ 
    self = [super initWithFrame:frame]; 
    if (self) { 
     // Initialization code 
     [self setBackgroundColor:[UIColor yellowColor]]; 
     UIScrollView* scrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 35, 600, 600)]; 
     [scrollView setScrollEnabled:YES]; 
     scrollView.pagingEnabled = YES; 
     scrollView.backgroundColor = [UIColor blueColor]; 
     [scrollView setShowsHorizontalScrollIndicator:YES]; 
     [scrollView setIndicatorStyle:UIScrollViewIndicatorStyleDefault]; scrollView.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight; 
     scrollView.contentMode = UIViewContentModeScaleToFill; 
     [scrollView setContentSize:CGSizeMake(630,0)]; 

     [self addSubview:scrollView]; 



    } 
    return self; 
} 

加入VC作爲

TestView *view=[[TestView alloc]initWithFrame:CGRectMake(20, 30, 500, 600)]; 

[self.view addSubview:view]; 
+0

他提到,他只需要水平滾動條下面的答案。這不是問題。 –

相關問題