需要設(shè)置父view的 clipsToBounds = YES
作用是 視圖上的子視圖,如果超出父視圖的部分就截取掉
- (void)viewDidLoad {
[super viewDidLoad];
[self addviews];
}
- (void)addviews {
mainview = [[UIView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, 200)];
[self.view addSubview:mainview];
mainview.backgroundColor = [UIColor grayColor];
//視圖上的子視圖,如果超出父視圖的部分就截取掉,
mainview.clipsToBounds = YES;
UIView *sub1 = [[UIView alloc] initWithFrame:CGRectMake(0, 100, 100, 100)];
[mainview addSubview:sub1];
sub1.backgroundColor = [UIColor yellowColor];
}
- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event {
CGRect frame = mainview.frame;
[UIView animateWithDuration:2.0 animations:^{
mainview.frame = CGRectMake(frame.origin.x, frame.origin.y, frame.size.width, 10);
}];
}