iOS最小化视图和左上角的所有子视图

iOS最小化视图和左上角的所有子视图,ios,uiview,uiviewanimation,Ios,Uiview,Uiviewanimation,我有一个ui视图,其中包含一些子视图(以及其中的一些子视图)。我试图最小化屏幕左上角的整个视图,直到它达到一定的大小(20 x 20),但我在这样做时遇到了麻烦 我首先尝试了[UIView beginAnimations:nil context:nil]但这并没有调整子视图的大小。此外,由于其他子视图的填充和数量,很难将每个子视图框架设置为新的大小并使其正常工作 然后我尝试了以下代码: [UIView beginAnimations:nil context:nil]; [UIView setAn

我有一个
ui视图
,其中包含一些子视图(以及其中的一些子视图)。我试图最小化屏幕左上角的整个视图,直到它达到一定的大小(20 x 20),但我在这样做时遇到了麻烦

我首先尝试了
[UIView beginAnimations:nil context:nil]但这并没有调整子视图的大小。此外,由于其他子视图的填充和数量,很难将每个子视图框架设置为新的大小并使其正常工作

然后我尝试了以下代码:

[UIView beginAnimations:nil context:nil];
[UIView setAnimationDuration:2.0f];
[UIView setAnimationCurve:UIViewAnimationCurveEaseOut];

[self.myCardsCarousel.layer setAnchorPoint:CGPointMake(0.0f, 0.0f)];
[self.myCardsCarousel setTransform:CGAffineTransformMakeScale(0.0f, 0.0f)];

CGRect addCardsButtonFrame = self.addCardsButton.frame;
addCardsButtonFrame.size.height = 0.0f;

[self.addCardsButton setFrame:addCardsButtonFrame];

[UIView commitAnimations];
但这并没有最小化到屏幕左上角。相反,它最小化到屏幕的死点


如何将其锚定到屏幕左上角?此外,当视图达到一定大小时,如何停止
cGraffineTransformMakeScale()

一个可能的解决方案是创建视图的快照,并将快照动画化到角落(假设最小化视图可以是静态的)。事件的一般顺序是

  • 在内存位图中创建父视图的快照
  • 从快照创建UIImageView
  • 将UIImageView放置在父视图前面
  • 使用父视图的
    hidden
    属性隐藏父视图
  • 将UIImageView设置为角点的动画
恢复视图的步骤

// create a snapshot image of the parent view
UIGraphicsBeginImageContextWithOptions( self.parentView.bounds.size, YES, 0 );
CGContextRef context = UIGraphicsGetCurrentContext();
[self.parentView.layer renderInContext:context];
UIImage *snapshot = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();

// create a UIImageView from the snapshot
self.snapshotView = [[UIImageView alloc] initWithFrame:self.parentView.frame];
self.snapshotView.image = snapshot;
[self.view addSubview:self.snapshotView];

// hide the parent view in place, while animating the snapshot view into the corner
self.parentView.hidden = YES;
[UIView animateWithDuration:0.5 delay:0 options:UIViewAnimationOptionCurveEaseInOut
animations:^
{
     self.snapshotView.frame = CGRectMake( 0, 0, 20, 20 );
}
completion:nil];
[UIView animateWithDuration:0.5 delay:0 options:UIViewAnimationOptionCurveEaseInOut
animations:^
{
    // animate the snapshot view back to original size
    self.snapshotView.frame = self.parentView.frame;
}
completion:^(BOOL finished)
{
    // reveal the parent view and discard the snapshot view
    self.parentView.hidden = NO;
    [self.snapshotView removeFromSuperview];
    self.snapshotView = nil;
}];
  • 将UIImageView设置回其原始位置的动画
  • 取消隐藏父视图
  • 卸下UIImageView并丢弃它
首先,需要UIImageView的属性

@property (strong, nonatomic) UIImageView *snapshotView;
下面是最小化视图的代码

// create a snapshot image of the parent view
UIGraphicsBeginImageContextWithOptions( self.parentView.bounds.size, YES, 0 );
CGContextRef context = UIGraphicsGetCurrentContext();
[self.parentView.layer renderInContext:context];
UIImage *snapshot = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();

// create a UIImageView from the snapshot
self.snapshotView = [[UIImageView alloc] initWithFrame:self.parentView.frame];
self.snapshotView.image = snapshot;
[self.view addSubview:self.snapshotView];

// hide the parent view in place, while animating the snapshot view into the corner
self.parentView.hidden = YES;
[UIView animateWithDuration:0.5 delay:0 options:UIViewAnimationOptionCurveEaseInOut
animations:^
{
     self.snapshotView.frame = CGRectMake( 0, 0, 20, 20 );
}
completion:nil];
[UIView animateWithDuration:0.5 delay:0 options:UIViewAnimationOptionCurveEaseInOut
animations:^
{
    // animate the snapshot view back to original size
    self.snapshotView.frame = self.parentView.frame;
}
completion:^(BOOL finished)
{
    // reveal the parent view and discard the snapshot view
    self.parentView.hidden = NO;
    [self.snapshotView removeFromSuperview];
    self.snapshotView = nil;
}];
下面是恢复视图的代码

// create a snapshot image of the parent view
UIGraphicsBeginImageContextWithOptions( self.parentView.bounds.size, YES, 0 );
CGContextRef context = UIGraphicsGetCurrentContext();
[self.parentView.layer renderInContext:context];
UIImage *snapshot = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();

// create a UIImageView from the snapshot
self.snapshotView = [[UIImageView alloc] initWithFrame:self.parentView.frame];
self.snapshotView.image = snapshot;
[self.view addSubview:self.snapshotView];

// hide the parent view in place, while animating the snapshot view into the corner
self.parentView.hidden = YES;
[UIView animateWithDuration:0.5 delay:0 options:UIViewAnimationOptionCurveEaseInOut
animations:^
{
     self.snapshotView.frame = CGRectMake( 0, 0, 20, 20 );
}
completion:nil];
[UIView animateWithDuration:0.5 delay:0 options:UIViewAnimationOptionCurveEaseInOut
animations:^
{
    // animate the snapshot view back to original size
    self.snapshotView.frame = self.parentView.frame;
}
completion:^(BOOL finished)
{
    // reveal the parent view and discard the snapshot view
    self.parentView.hidden = NO;
    [self.snapshotView removeFromSuperview];
    self.snapshotView = nil;
}];

抱歉,我没有足够的时间发布完整的答案,只是一些提示:如果您想缩放和移动视图,请使用组合变换,例如CGAffineTransformConcat(CGAffineTransformMakeScale(0.2,0.2),CGAffineTransformMakeTransform(-160,-320)),一旦动画正在进行,您就不能停止它,当比例因子达到某个值时。只需计算正确的目标比例x和y因子,并将它们传递到CGAffineTransformMakeScale函数中。