Ios 如何将UIImageView添加到UIView,而不显示UIImageView边框之外的内容
我试图将UIImageView添加到UIView中,问题是图像视图位于UIView框架之外,因此理论上应该将其隐藏(UIImageView中位于UIView之外的部分) 这是我的密码:Ios 如何将UIImageView添加到UIView,而不显示UIImageView边框之外的内容,ios,objective-c,animation,uiview,uiimageview,Ios,Objective C,Animation,Uiview,Uiimageview,我试图将UIImageView添加到UIView中,问题是图像视图位于UIView框架之外,因此理论上应该将其隐藏(UIImageView中位于UIView之外的部分) 这是我的密码: - (void)viewDidLoad { [super viewDidLoad]; // Do any additional setup after loading the view, typically from a nib. firstRectangle = [[UIView al
- (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) {
}];
}
修复了所有问题,以下是代码:
- (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属性添加到代码中
- 然后我只是调整了代码以获得想要的动画,我希望这些代码将帮助其他人实现过渡或不同的动画