需要设置父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);
}];
}
网友评论