[英]flip two subview doesn't work continuosly
I have found a solution here How to flip an individual UIView (without flipping the parent view) 我在这里找到了一个解决方案, 如何翻转单个UIView(不翻转父视图)
but it seems that this doesn't work for me. 但这似乎对我不起作用。 In Interface Builder i have set up all with a uiview and inside that two uiviews, with the same size and the first view to be shown on top and i have created three outlets for this 3 views.
在Interface Builder中,我都设置了一个uiview,并在两个uiview中都设置了相同的大小,并且第一个视图显示在顶部,并且我为这3个视图创建了三个出口。
In viewDidLoad: 在viewDidLoad中:
[self.view addSubview:self.contentView]
[self.contentView addSubview:firstView]
[self.contentView addSubview:secondView]
i have a uigesture. 我有一个ugesture。 When the user taps the screen, the IBAction executes the flip:
当用户点击屏幕时,IBAction执行翻转:
-(IBAction)flip:(id *)sender
{
if(faceUp){
// execute transition from firstview to second
}else{
//execute transition from secondview to first
}
faceUp = !faceUp;
}
this work perfectly the first time i click and the flips happens from the first to second, then if i tap again the flip starts and then it ends with nothing, the view disappears and remains the container view showing nothing because its color is set to clear. 第一次单击时此功能完美无缺,并且翻转发生在第一到第二次之间,然后如果我再次轻按翻转开始,然后一切都没有结束,则视图消失并保持容器视图不显示任何内容,因为其颜色设置为清除。 How can i resolve this?
我该如何解决?
This is the controller with the tap gesture method and the three outlets: 这是带有轻击手势方法和三个插座的控制器:
#import "ContactViewController.h"
#import "InfoContactView.h"
#import "ImageLogoView.h"
@interface ContactViewController ()
@property (strong, nonatomic) IBOutlet InfoContactView *contactInfoView;
@property (strong, nonatomic) IBOutlet ImageLogoView *imageLogoView;
@property (nonatomic) BOOL faceUp;
@property (strong, nonatomic) IBOutlet UIView *parentView;
@end
@implementation ContactViewController
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
// Custom initialization
}
return self;
}
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view.
self.parentView.backgroundColor =[UIColor clearColor];
[self.view addSubview:self.parentView];
[self.parentView addSubview:self.contactInfoView];
[self.parentView addSubview:self.imageLogoView];
self.faceUp = NO;
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
-(void)setFaceUp:(BOOL)faceUp
{
_faceUp = faceUp;
}
-(void)setParentView:(UIView *)parentView
{
_parentView =parentView;
}
-(void)setContactInfoView:(InfoContactView *)contactInfoView
{
_contactInfoView = contactInfoView;
}
-(void)setImageLogoView:(ImageLogoView *)imageLogoView
{
_imageLogoView = imageLogoView;
}
- (IBAction)showInfo:(UITapGestureRecognizer *)sender {
if (self.faceUp == NO) {
[UIView transitionFromView:self.imageLogoView toView:self.contactInfoView
duration:1.5
options:UIViewAnimationOptionTransitionFlipFromLeft
completion:NULL];
NSLog(@"Pass from logo to info : imageLogoView = %@",self.imageLogoView.description);
NSLog(@"dettagli = %@",self.contactInfoView);
}
else if(self.faceUp == YES){
[UIView transitionFromView:self.contactInfoView toView:self.imageLogoView
duration:1.5
options:UIViewAnimationOptionTransitionFlipFromRight
completion:NULL];
NSLog(@"Pass from info to logo: imageLogoView = %@",self.imageLogoView.description);
NSLog(@"dettagli = %@",self.contactInfoView);
}
self.faceUp = !self.faceUp;
}
@end
I have tried to reconstruct your problem in Xcode. 我试图用Xcode重建您的问题。 I added some lines of code to log
parentView
's subviews before and after each transition. 我在每次转换之前和之后添加了一些代码行来记录
parentView
的子视图。 Judging from the logs your initial view's frame ( self.imageLogoView
) is reset to a width and height of 0 at some point: 从日志中判断,初始视图的框架(
self.imageLogoView
)在某个点被重置为宽度和高度0:
<UIView: 0x8b84310; frame = (159.5 284.5; 0 0); autoresize = W+H; layer = <CALayer: 0x8b84370>>
which means it is still there but you just can't see it. 这意味着它仍然存在,但您看不到它。
Apparently Autolayout is responsible for resizing the view's frame . 显然,自动布局负责调整视图框架的大小 。 If you deactivate Autolayout for your View Controller the transition works just fine.
如果您为View Controller停用自动布局,则过渡效果很好。 (To deactivate Autolayout select the respective View Controller in your Storyboard. In the right column (Utilities) go to the first tab
(要停用自动布局,请在情节提要中选择相应的View Controller。在右列(实用程序)中转到第一个标签
and deselect the checkbox "Use Autolayout" .
并取消选中“使用自动版式”复选框。
)
)
On the other hand Autolayout is said to be a nice thing or at least it is promoted by Apple as such. 另一方面,Autolayout被认为是一件好事,或者至少由Apple这样推广。 So if you want to use Autolayout you have to take care of your view's frames yourself by adding appropriate constraints.
因此,如果要使用自动版式,则必须通过添加适当的约束来自己照顾视图的框架。 First select your
imageLogoView
and add constraints for width and height by checking the respective boxes as shown in the screenshot below. 首先选择您的
imageLogoView
并通过选中相应的框来添加宽度和高度的约束,如下面的屏幕快照所示。 Then do the same with your contactInfoView
. 然后对您的
contactInfoView
进行相同的contactInfoView
。
That should do the trick. 这应该够了吧。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.