我正在尝试将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) {
}];
}
答案 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) {
}];
}