2015-09-12 121 views
0

實際上有四個按鈕,每個按鈕都有子按鈕。一旦我點擊第一個按鈕,第二個按鈕必須通過爲子按鈕提供空間而向下移動,但即使在設置了滾動視圖之後它也不能工作。滾動視圖中的問題

 bgsroll=[[UIScrollView alloc]init]; 
     bgsroll.frame=CGRectMake(0, 0, CGRectGetWidth(self.view.frame), CGRectGetHeight(self.view.frame)); 
     bgsroll.userInteractionEnabled=YES; 
     bgsroll.scrollEnabled=YES; 
     // bgsroll.backgroundColor=[UIColor grayColor]; 
     bgsroll.showsVerticalScrollIndicator=YES; 
     bgsroll.contentSize=CGSizeMake(200, 2000); 
     [self.view addSubview:bgsroll]; 




      happyBtn=[[UIButton alloc]init]; 
      happyBtn.frame=CGRectMake(MainView.frame.size.width*0.12, MainView.frame.size.height*0.1, CGRectGetWidth(self.view.frame)/1.5, 40); 
      [happyBtn setTitle:@"Happiness" forState:UIControlStateNormal]; 
      [happyBtn setBackgroundColor:[UIColor colorWithRed:0.400f green:0.737f blue:0.761f alpha:1.00f]]; 
      [happyBtn addTarget:self action:@selector(tapHappy:) forControlEvents:UIControlEventTouchUpInside]; 
      [bgsroll addSubview:happyBtn]; 

      happyView=[[UIView alloc]init]; 
      happyView.frame=CGRectMake(MainView.frame.size.width*0.26,CGRectGetHeight(happyBtn.frame)*2.2, CGRectGetWidth(MainView.frame)/1.6, CGRectGetHeight(MainView.frame)/3); 
      happyView.layer.cornerRadius=8.0; 
      happyView.layer.borderWidth=0.5; 
      happyView.layer.borderColor=[[UIColor blueColor]CGColor]; 
      [bgsroll addSubview:happyView]; 


      LettingGo=[[UIButton alloc]init]; 
      LettingGo.frame=CGRectMake(happyView.frame.size.width*0.01,happyView.frame.size.height*0.1, CGRectGetWidth(happyView.frame)/1.05, 50); 
      [LettingGo setTitle:@"Letting go of negativity" forState:UIControlStateNormal]; 
      LettingGo.titleLabel.numberOfLines=2; 
      LettingGo.titleLabel.adjustsFontSizeToFitWidth=YES; 
      [LettingGo setBackgroundColor:[UIColor colorWithRed:0.400f green:0.737f blue:0.761f alpha:1.00f]]; 
      [happyView addSubview:LettingGo]; 


      LivingPresent=[[UIButton alloc]init]; 
      LivingPresent.frame=CGRectMake(happyView.frame.size.width*0.01,LettingGo.frame.size.height*1.4, CGRectGetWidth(happyView.frame)/1.05, 40); 
      [LivingPresent setTitle:@"Living in the present" forState:UIControlStateNormal]; 
      LivingPresent.titleLabel.adjustsFontSizeToFitWidth=YES; 
      [LivingPresent setBackgroundColor:[UIColor colorWithRed:0.400f green:0.737f blue:0.761f alpha:1.00f]]; 
      [happyView addSubview:LivingPresent]; 
      happyView.hidden=YES; 


      CreateBtn=[[UIButton alloc]init]; 
      CreateBtn.frame=CGRectMake(MainView.frame.size.width*0.12, MainView.frame.size.height*0.5, CGRectGetWidth(self.view.frame)/1.5, 40); 
      [CreateBtn setTitle:@"Happiness" forState:UIControlStateNormal]; 
      [CreateBtn setBackgroundColor:[UIColor colorWithRed:0.400f green:0.737f blue:0.761f alpha:1.00f]]; 
      [CreateBtn addTarget:self action:@selector(tapHappy:) forControlEvents:UIControlEventTouchUpInside]; 
      [bgsroll addSubview:CreateBtn]; 


    -(void)tapHappy:(id)selector{ 

      happyView.hidden=!happyView.hidden; 

    } 
+0

你能分享我們如何「移動」你的按鈕,我不能在這裏看到滾動視圖? –

+0

您現在可以看到 – Arun

+0

您還可以分享tapHappy:按鈕操作代碼嗎? –

回答

0

通過不工作,你的意思是,當按鈕被按下時,它不滾動?我發現更容易滾動元素來創建一個方法,通過動畫改變子視圖中元素的座標。這樣的事情:

- (void)viewDidLoad { 
     expandButton1 = [UIButton buttonWithType:UIButtonTypeCustom]; 
     [expandButton1 addTarget:self action:@selector(expandButtonPressed:) forControlEvents:UIControlEventTouchUpInside]; 
} 

- (void)expandButtonPressed:(id)sender{ 
     if (expanded == NO) { 
     expanded = YES; 

     [UIView animateWithDuration:0.5 delay:0.0 options:UIViewAnimationOptionRepeat animations:^{ 
     [UIView setAnimationRepeatCount:1];    
     image1.frame = CGRectMake(image1.frame.origin.x, image1.frame.origin.y+200.0f, image1.frame.size.width, image1.frame.size.height); 
     image.frame2 = CGRectMake(image2.frame.origin.x, image2.frame.origin.y+200.0f, image2.frame.size.width, image2.frame.size.height); 
     expandButton2.frame = CGRectMake(expandButton2.frame.origin.x, expandButton2.frame.origin.y+200.0f, expandButton2.frame.size.width, expandButton2.frame.size.height); 
      expandButton3.frame = CGRectMake(expandButton3.frame.origin.x, expandButton3.frame.origin.y+200.0f, expandButton3.frame.size.width, expandButton3.frame.size.height); 

     }completion:nil]; 
}