如何在不显示UIViewBorder之外的UIImageView内容的情况下将UIImageView添加到UIView

时间:2015-04-01 14:19:50

标签: ios objective-c animation uiview uiimageview

我正在尝试将UIImageView添加到UIView,问题是图像视图在UIViews框架之外,所以理论上它应该被隐藏(UIImageView的一部分位于UIView之外)。 / p>

这是我的代码:

- (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 个答案:

答案 0 :(得分:-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和Bounds属性添加到我的代码中。
  • 然后我只是调整代码来获取希望的动画,我希望这些代码可以帮助其他人实现转换或不同的动画。