我在iOS上很新。在我的项目中,我添加了两个UIView
xib文件,它们是“View1”xib文件和“View2”xib文件,在这里我在View1 xib文件中添加了一个按钮。
当我点击该按钮时,我想使用UIView
动画移动下一个UIView
xib文件,即View2(就像我们将一个UIViewController
推送到另一个UIViewcontroller
一样#import "Test.h"
@implementation Test
@synthesize MainArray;
-(instancetype)initWithCoder:(NSCoder *)aDecoder{
if (self = [super initWithCoder:aDecoder]) {
[self loadingView];
}
return self;
}
-(instancetype)initWithFrame:(CGRect)frame{
if (self = [super initWithFrame:frame]) {
[self loadingView];
}
return self;
}
-(void)loadingView{
UIView * MainView = [[[NSBundle bundleForClass:[self class]]loadNibNamed:@"View1" owner:self
options:nil]firstObject];
[self addSubview:MainView];
MainView.frame = self.bounds;
UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
[button addTarget:self
action:@selector(aMethod:)
forControlEvents:UIControlEventTouchUpInside];
[button setTitle:@"Show View" forState:UIControlStateNormal];
button.backgroundColor = [UIColor blackColor];
button.frame = CGRectMake(80.0, 110.0, 100.0, 30.0);
[MainView addSubview:button];
}
-(void)aMethod :(id)sender{
//here when i click i want to move next UIView (i.e VIew2 xib file)
}
)
$, = "\t"
答案 0 :(得分:0)
-(void)aMethod :(id)sender{
//here when i click i want to move next UIView (i.e VIew2 xib file)
UIView * view2 = [[[NSBundle bundleForClass:[self class]]loadNibNamed:@"View2" owner:self
options:nil]firstObject];
[UIView transitionWithView:containerView duration:1
options:UIViewAnimationOptionTransitionCurlUp //change animation here
animations:^ {
[self addSubview:view2];
}
completion:nil];
}
或
-(void)aMethod :(id)sender{
//here when i click i want to move next UIView (i.e VIew2 xib file)
UIView * view2 = [[[NSBundle bundleForClass:[self class]]loadNibNamed:@"View2" owner:self
options:nil]firstObject];
CATransition *transition = [CATransition animation];
transition.duration = 1.0;
transition.type = kCATransitionPush; //change animation here
[view2.layer addAnimation:transition forKey:nil];
[self addSubview:view2];
}
或
[self addSubview:view2];
[view2 setFrame:CGRectMake( 0.0f, 480.0f, 320.0f, 480.0f)];
[UIView beginAnimations:@"animateView" context:nil];
[UIView setAnimationDuration:1];
[view2 setFrame:CGRectMake( 0.0f, 0.0f, 320.0f, 480.0f)]; // move in
[UIView commitAnimations];
或
[self.view addSubview:view2];
[view2 setFrame:CGRectMake( 0.0f, 480.0f, 320.0f, 480.0f)];
[UIView animateWithDuration:1 animations:^{
[view2 setFrame:CGRectMake( 0.0f, 0.0f, 320.0f, 480.0f)]; // move in
}];