我有一个包含两个子视图的视图。我有它工作,所以一次只显示一个子视图,每个子视图都有一个按钮,当点击按钮时,子视图将翻转,下一个子视图将出现。问题是看起来似乎整个视图都在翻转。在这个网站上阅读有关如何解决问题后,我尝试将子视图添加到容器中并翻转它。但是现在,虽然当我按下按钮时我的第一个子视图显示出来但它不再翻转。它没有做任何事情。我在方法中放了一个日志语句来翻转子视图,以及一个断点,据我所知,它不再被调用。我对xcode和目标c和代表都很新,我不知道如何继续。任何帮助,将不胜感激。感谢。
我在此处列出了相关代码: ViewController的标头
@interface ExerciseViewController : UIViewController<ExerciseSubViewDelegate>
//stuff for subviews
@property (nonatomic, strong) ExerciseSubViewImage *subViewImage;
@property (nonatomic, strong) ExerciseSubViewText *subViewText;
@property UIView *panel;
@end
这是ViewController的代码:
@interface ExerciseViewController ()
@end
@implementation ExerciseViewController
@synthesize subViewImage, subViewText;
- (void)viewDidLoad
{
self.subViewImage.delegate = self;
_panel = [[UIView alloc] initWithFrame:CGRectMake(0,0, self.view.bounds.size.width, self.view.bounds.size.height/2)];
_panel.backgroundColor = [UIColor whiteColor];
[self.view addSubview:_panel];
[_panel addSubview:subViewImage];
}
-(ExerciseSubViewImage *)subViewImage
{
if (!subViewImage)
{
CGRect subViewImageFrame = CGRectMake(0,0, _panel.bounds.size.width, _panel.bounds.size.height);
self.subViewImage = [[ExerciseSubViewImage alloc] initWithFrame:subViewImageFrame];
[_panel addSubview:subViewImage];
}
return subViewImage;
}
-(ExerciseSubViewText *)subViewText
{
if (!subViewText)
{
CGRect subViewTextFrame = CGRectMake(0,0, _panel.bounds.size.width, _panel.bounds.size.height);
self.subViewText = [[ExerciseSubViewText alloc] initWithFrame:subViewTextFrame];
self.subViewText.backgroundColor = [UIColor blueColor];
[_panel addSubview:subViewText];
}
return subViewText;
}
-(void)exerciseSubViewImagePressed
{
[UIView transitionWithView:_panel
duration:0.2
options:UIViewAnimationOptionTransitionFlipFromRight
animations:^{
[subViewImage removeFromSuperview];
[_panel addSubview:subViewText];
}
completion: nil];
//This is how I did it before I added the container
/*[UIView transitionFromView:subViewImage
toView:subViewText
duration:0.2
options:UIViewAnimationOptionTransitionFlipFromRight
completion:nil];
self.subViewText.delegate = self;*/
NSLog(@"Ipushedtheimage");
}
-(void)exerciseSubViewTextPressed
{//I haven't updated this yet
[UIView transitionFromView:subViewText
toView:subViewImage
duration:0.2
options:UIViewAnimationOptionTransitionFlipFromRight
completion:nil];
self.subViewImage.delegate = self;
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
subViewImage = nil;
subViewText = nil;
}
@end
这是代理人的代码 #import
@protocol ExerciseSubViewDelegate <NSObject>
-(void) exerciseSubViewImagePressed;
-(void) exerciseSubViewTextPressed;
@end 我还添加了第一个子视图的代码: #进口 #import“ExerciseSubViewDelegate.h”
@interface ExerciseSubViewImage : UIView
@property (nonatomic, strong) UIButton *button;
@property (nonatomic, assign) id<ExerciseSubViewDelegate>delegate;
@property (strong, nonatomic) UIImageView *exerciseImageView;
@end
#import "ExerciseSubViewImage.h"
#import "UIImage+animatedGIF.h"
@implementation ExerciseSubViewImage
@synthesize button;
@synthesize delegate;
- (id)initWithFrame:(CGRect)frame
{
self = [super initWithFrame:frame];
if (self) {
//Initialization code
self.button = [UIButton buttonWithType:UIButtonTypeRoundedRect];
CGRect buttonFrame = CGRectMake(50,200,100,35);
self.button.frame = buttonFrame;
[self.button setTitle:@"Image"forState:UIControlStateNormal];
[self.button addTarget:self
action:@selector(buttonTouched)
forControlEvents:UIControlEventTouchUpInside];
[self addSubview:self.button];
_exerciseImageView = [[UIImageView alloc] initWithFrame:CGRectMake(50,20,160,158)];
NSURL *url = [[NSBundle mainBundle] URLForResource:@"AppleLogo" withExtension:@"gif"];
_exerciseImageView.image = [UIImage animatedImageWithAnimatedGIFURL:url];
[self addSubview:self.exerciseImageView];
}
return self;
}
-(void)buttonTouched
{
NSLog(@"imagebuttonpressed");
[self.delegate exerciseSubViewImagePressed];
}
再一次,任何帮助都会受到赞赏。我知道我可能只是不理解简单的东西。
答案 0 :(得分:1)
确定。这花了我整个周末,但我终于自己想出来了。我想我会在这里找到答案,万一其他人有类似的问题。在尝试了其他几种方法之后,我终于回到了我在这里使用的方法,并开始插入一大堆NSLog来确定每个东西执行的顺序。我最终做的是改变这个:(全部在顶部的ViewController中) )
self.subViewImage.delegate = self;
_panel = [[UIView alloc] initWithFrame:CGRectMake(0,0, self.view.bounds.size.width, self.view.bounds.size.height/2)];
_panel.backgroundColor = [UIColor whiteColor];
[self.view addSubview:_panel];
[_panel addSubview:subViewImage];
到此:
//create panel
_panel = [[UIView alloc] initWithFrame:CGRectMake(0,0, self.view.bounds.size.width, s self.view.bounds.size.height/2)];
_panel.backgroundColor = [UIColor whiteColor];
[self.view addSubview:_panel];
[_panel addSubview:subViewImage];
//Set the subview delegates
self.subViewImage.delegate = self;
self.subViewText.delegate = self;