2015-04-01 45 views
-1

我想將UIImageView添加到UIView,問題在於圖像視圖不在UIViews框架中,所以理論上它應該被隱藏(位於UIView之外的UIImageView部分)。如何將UIImageView添加到UIView而不顯示UIViewBorder之外的UIImageView的內容

這裏是我的代碼:

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

    firstRectangle = [[UIView alloc] initWithFrame:CGRectMake(0, [[UIScreen mainScreen] bounds].size.height, [[UIScreen mainScreen] bounds].size.width, [[UIScreen mainScreen] bounds].size.height)]; 
    firstRectangle.hidden = NO; 
    firstRectangle.backgroundColor = [UIColor yellowColor]; 
    [self.view addSubview:firstRectangle]; 
    [firstRectangle setContentMode:UIViewControllerHierarchyInconsistencyException]; 

    secondRectangle = [[UIImageView alloc] initWithFrame:CGRectMake(0, -[[UIScreen mainScreen] bounds].size.height, [[UIScreen mainScreen] bounds].size.width, [[UIScreen mainScreen] bounds].size.height)]; 
    secondRectangle.hidden = NO; 
    secondRectangle.backgroundColor = [UIColor redColor]; 
    [firstRectangle addSubview:secondRectangle]; 

    UIButton *animateButton = [[UIButton alloc]initWithFrame:CGRectMake([[UIScreen mainScreen] bounds].size.width/2, [[UIScreen mainScreen] bounds].size.height/2, 100, 30)]; 
    animateButton.backgroundColor = [UIColor greenColor]; 
    [animateButton addTarget:self action:@selector(animateButtonFunction) forControlEvents:UIControlEventTouchUpInside]; 
    [self.view addSubview:animateButton]; 
} 

- (void)animateButtonFunction{ 

[UIView animateWithDuration:10.f 
         delay:0.0f 
        options:UIViewAnimationOptionCurveEaseOut 
       animations:^{ 

        [firstRectangle setFrame:CGRectMake(0, 0, [[UIScreen mainScreen] bounds].size.width, [[UIScreen mainScreen] bounds].size.height)]; 
        [secondRectangle setFrame:CGRectMake(0, 0, [[UIScreen mainScreen] bounds].size.width, [[UIScreen mainScreen] bounds].size.height)]; 

       } 
       completion:^(BOOL completed) { 



       }]; 

} 

回答

-1

修復所有問題,這裏是代碼:

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

    firstRectangle = [[UIView alloc] initWithFrame:CGRectMake(0, [[UIScreen mainScreen] bounds].size.height, [[UIScreen mainScreen] bounds].size.width, [[UIScreen mainScreen] bounds].size.height)]; 
    firstRectangle.hidden = NO; 
    firstRectangle.backgroundColor = [UIColor yellowColor]; 
    firstRectangle.bounds = CGRectMake(0, [[UIScreen mainScreen] bounds].size.height, [[UIScreen mainScreen] bounds].size.width, [[UIScreen mainScreen] bounds].size.height); 
    firstRectangle.clipsToBounds = YES; 
    [self.view addSubview:firstRectangle]; 

    secondRectangle = [[UIView alloc] initWithFrame:CGRectMake(0, 0, [[UIScreen mainScreen] bounds].size.width, [[UIScreen mainScreen] bounds].size.height)]; 
    secondRectangle.hidden = NO; 
    secondRectangle.backgroundColor = [UIColor redColor]; 
    secondRectangle.layer.borderWidth = 5.f; 
    secondRectangle.layer.borderColor = [[UIColor greenColor] CGColor]; 
    [firstRectangle addSubview:secondRectangle]; 

    UIButton *animateButton = [[UIButton alloc]initWithFrame:CGRectMake([[UIScreen mainScreen] bounds].size.width/2, [[UIScreen mainScreen] bounds].size.height/2, 100, 30)]; 
    animateButton.backgroundColor = [UIColor greenColor]; 
    [animateButton addTarget:self action:@selector(animateButtonFunction) forControlEvents:UIControlEventTouchUpInside]; 
    [self.view addSubview:animateButton]; 
} 

- (void)animateButtonFunction{ 
NSLog(@"test"); 
[UIView animateWithDuration:.6f 
         delay:0.0f 
        options:UIViewAnimationOptionCurveLinear 
       animations:^{ 

        [firstRectangle setBounds:CGRectMake(0, 0, [[UIScreen mainScreen] bounds].size.width, [[UIScreen mainScreen] bounds].size.height)]; 
        [firstRectangle setFrame:CGRectMake(0, 0, [[UIScreen mainScreen] bounds].size.width, [[UIScreen mainScreen] bounds].size.height)]; 

       } 
       completion:^(BOOL completed) { 



       }]; 

} 
  • 的問題之一是,我需要添加clipToBounds和邊界性質到我的代碼。
  • 然後我只是調整了代碼來獲得希望的動畫,我希望這些代碼可以幫助其他人實現轉換或不同的動畫。
相關問題