Because of the project requirements, we need a simple animation like the following:
#### single control This animation effect can be achieved by using a UIView block animation on the control and setting the control’s Frame value.
For example:
- (void)viewDidLoad {
[super viewDidLoad];
self.btn = [[UIButton alloc] initWithFrame:CGRectMake(100, 100, 200, 200)];
[self.btn setImage:[UIImage imageNamed:@"1"] forState:UIControlStateNormal];
[self.view addSubview:self.btn];
}
- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event
{
[UIView animateWithDuration: 1.0 animations: ^ {
self.btn.frame = CGRectMake(100, 300, 200, 0);
}];
}
Copy the code
#### How about multiple controls? Let’s do the same thing, put BTN in a View, and then we use a UIView block animation for the View. First look at the code:
- (void)viewDidLoad {
[super viewDidLoad];
self.outView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, [UIScreen mainScreen].bounds.size.width, [UIScreen mainScreen].bounds.size.height-44)];
self.outView.backgroundColor = [UIColor redColor];
self.btn = [[UIButton alloc] initWithFrame:CGRectMake(100, 100, 200, 200)];
[self.btn setImage:[UIImage imageNamed:@"1"] forState:UIControlStateNormal];
[self.outView addSubview:self.btn];
[self.view addSubview:self.outView];
}
- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event
{
[UIView animateWithDuration: 1.0 animations: ^ {
self.outView.frame = CGRectMake(0, [UIScreen mainScreen].bounds.size.height-44, [UIScreen mainScreen].bounds.size.width, 0);
}];
}
Copy the code
Then let’s look at the effect:
self.outView.frame = CGRectMake(0, [UIScreen mainScreen].bounds.size.height-44, [UIScreen mainScreen].bounds.size.width, 0);
Self. OutView. Layer. The affineTransform = CGAffineTransformScale (self. OutView. Layer. The affineTransform, 1.0, 0.001);
####anchorPoint, position, CGAffineTransform Yes, through these three things, first we upload the code and implementation diagram, and then analyze.
- (void)viewDidLoad {
[super viewDidLoad];
self.outView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, [UIScreen mainScreen].bounds.size.width, [UIScreen mainScreen].bounds.size.height-44)];
self.outView.backgroundColor = [UIColor redColor];
self.btn = [[UIButton alloc] initWithFrame:CGRectMake(100, 100, 200, 200)];
[self.btn setImage:[UIImage imageNamed:@"1"] forState:UIControlStateNormal];
[self.outView addSubview:self.btn];
[self.view addSubview:self.outView];
}
- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event
{
// Note that self.outview. frame cannot be used here because the frame changes as the control changes.
// This causes the position of the layer to be inaccurate.
CGFloat positionX = 0.5 * self. OutView. Bounds. Size. The width;
CGFloat positionY = 1.0 * self. OutView. Bounds. The size, height;
The self. The outView. Layer. AnchorPoint = CGPointMake (0.5, 1);
self.outView.layer.position = CGPointMake(positionX, positionY);
[UIView animateWithDuration: 1.0 animations: ^ {
Self. OutView. Layer. The affineTransform = CGAffineTransformScale (self. OutView. Layer. The affineTransform, 1, 0.001);
}];
}
Copy the code
Effect:
outView