Iphone UIScrollView上的翻转子视图不工作
我正在用UiNavigation编写一个iPhone选项卡项目,第一个视图包含UIScrollView,滚动视图包含视图每个子视图包含imageview、anUiImage和一个UiButton 是构建第一个屏幕的函数Iphone UIScrollView上的翻转子视图不工作,iphone,ios5,uiview,uiscrollview,uiviewanimation,Iphone,Ios5,Uiview,Uiscrollview,Uiviewanimation,我正在用UiNavigation编写一个iPhone选项卡项目,第一个视图包含UIScrollView,滚动视图包含视图每个子视图包含imageview、anUiImage和一个UiButton 是构建第一个屏幕的函数 //******************************************************* //* Build the main screen * //**********************
//*******************************************************
//* Build the main screen *
//*******************************************************
-(void) buildScreen{
sing.viewsArray = [[NSMutableArray alloc]init];
int Vve = 10;
int Vho = 10;
int wi = 95;
int hi = 95;
int ArrayIndex = 0;
for (int i = 0; i < 5; i++) {
for (int j = 0; j < 3; j++) {
staticView = [[UIView alloc] initWithFrame:CGRectMake(Vve, Vho, 100, 100)];
[staticView setTag:ArrayIndex];
staticImage = [[UIImageView alloc] initWithImage:[UIImage imageNamed:[sing.stdPicArray objectAtIndex:ArrayIndex]]];
[staticImage setFrame:CGRectMake(0, 0, wi, hi)];
[staticView addSubview:staticImage];
viewButton = [UIButton buttonWithType:UIButtonTypeCustom];
viewButton.frame = CGRectMake(0, 0, wi, hi);
[viewButton addTarget:self action:@selector(buttonClick:) forControlEvents:UIControlEventTouchUpInside];
[viewButton setTag:ArrayIndex];
[sing.buttonsArray addObject:viewButton];
[staticView addSubview:viewButton];
[sing.viewsArray addObject:staticView];
[MainScroll addSubview:[sing.viewsArray objectAtIndex:ArrayIndex]];
Vve += 100;
ArrayIndex++;
}
Vve = 10;
Vho += 100;
}
}
不起作用,只是添加了flipViewBack
-(void)animateFlip{
[UIView beginAnimations:nil context:nil];
[UIView setAnimationDuration:1.0];
[UIView setAnimationTransition:UIViewAnimationTransitionFlipFromLeft
forView:self.flipViewFront
cache:YES];
[MainScroll addSubview:flipViewFront];
[UIView setAnimationDidStopSelector:@selector(flipAnimationDidStop:finished:context:)];
[UIView setAnimationDelegate:self];
[UIView commitAnimations];
}
没有像另一个那样工作,下一个代码确实用我想要的视图翻转了主视图,但它没有翻转只翻转我想要的视图
-(void)animateFlip{
[UIView beginAnimations:nil
context:nil];
[UIView setAnimationDuration:1.0];
[UIView transitionFromView:self.view
toView:flipViewBack
duration:2
options:UIViewAnimationOptionTransitionFlipFromBottom
completion:NULL];
[UIView setAnimationDidStopSelector:@selector(flipAnimationDidStop:finished:context:)];
[UIView setAnimationDelegate:self];
//set transformation
[UIView commitAnimations];
}
“有人知道怎么做吗?”我到处找,发现所有的代码都不适合我
谢谢
我尝试了这个链接中建议的另一个选项
但这对我不起作用
这是我在最后3行中单击时使用的函数,我将flipView添加到flipView(容器)中flipView前视图,然后我将flipView添加到主滚动视图中,然后我在从您的示例复制的animateFlip中执行动画
//*******************************************************
//* Click on the picture as button *
//*******************************************************
-(void) buttonClick:(id) sender{
UIButton *button = (UIButton *)sender;
int row = [button superview].tag;
NSLog(@"Button pressed tag: %i",row);
self.flipView = [[UIView alloc]initWithFrame:CGRectMake(65, 130, 190, 190)];
self.flipView.backgroundColor = [UIColor clearColor];
self.flipViewBack = [[UIView alloc]initWithFrame:CGRectMake(65, 130, 190, 190)];
self.flipViewFront = [[UIView alloc]initWithFrame:CGRectMake(65, 130, 190, 190)];
self.flipViewFront.backgroundColor = [UIColor whiteColor];
self.flipViewImage = [UIImage imageNamed:[sing.stdPicArray objectAtIndex:row]];
self.filpImage = [[UIImageView alloc]initWithImage:self.flipViewImage];
[self.flipViewBack addSubview:self.filpImage];
self.flipCloseButton = [UIButton buttonWithType:UIButtonTypeCustom];
[self.flipCloseButton setFrame:CGRectMake(0, 0, 24, 24)];
[self.flipCloseButton setImage:[UIImage imageNamed:@"close_button.png"] forState:UIControlStateNormal];
[self.flipCloseButton addTarget:self action:@selector(closwFlip:) forControlEvents:UIControlEventTouchUpInside];
[self.flipViewBack addSubview:flipCloseButton];
self.flipOkButton = [[UIButton alloc]initWithFrame:CGRectMake(30, 0, 190, 190)];
[self.flipCloseButton addTarget:self action:@selector(continueTo:) forControlEvents:UIControlEventTouchUpInside];
[self.flipViewBack addSubview:flipOkButton];
[flipView addSubview:flipViewFront];
[MainScroll addSubview:flipView];
[self animateFlip];
}
找到了问题,但没有找到答案,如果它直接从视图加载,并且第一个视图在屏幕上,则一切正常,翻转工作“如果我更改它,以便用户希望在一个事件中添加视图并翻转它,则会显示第二个(翻转)没有翻转的视图您的问题似乎是,在将翻转添加到视图层次结构之前,您试图在视图上设置翻转动画 相反,您应该将后视图添加到公共容器视图(稍后将包含前视图),然后在添加前视图和删除后视图的同时翻转容器视图,如下所示:
// Container view is a view that is already added to your view hierarchy.
// It contains the back view. The front view will be added to it.
[UIView transitionWithView:containerView
duration:1.0
options:UIViewAnimationOptionTransitionFlipFromLeft
animations:^{
[backView removeFromSuperview]; // This is already added to the container
[containerView addSubview:frontView]; // Not yet added ...
}
completion:NULL];
更新
这是我在回答这个问题时写的所有代码。它对我有用。如果它仍然无法为您设置动画,则您可能尚未以相同的方式设置视图层次
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
containerView = [[UIView alloc] initWithFrame:CGRectMake(30, 30, 100, 100)];
[self.view addSubview:containerView];
frontView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 100, 100)];
[frontView setBackgroundColor:[UIColor orangeColor]];
backView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 100, 100)];
[backView setBackgroundColor:[UIColor redColor]];
[containerView addSubview:backView];
UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self
action:@selector(flip)];
[self.view addGestureRecognizer:tap];
}
- (void)flip {
[UIView transitionWithView:containerView
duration:1.0
options:UIViewAnimationOptionTransitionFlipFromLeft
animations:^{
[backView removeFromSuperview];
[containerView addSubview:frontView];
}
completion:NULL];
}
您的问题似乎是,在将翻转添加到视图层次之前,您正在尝试对其设置动画 相反,您应该将后视图添加到公共容器视图(稍后将包含前视图),然后在添加前视图和删除后视图的同时翻转容器视图,如下所示:
// Container view is a view that is already added to your view hierarchy.
// It contains the back view. The front view will be added to it.
[UIView transitionWithView:containerView
duration:1.0
options:UIViewAnimationOptionTransitionFlipFromLeft
animations:^{
[backView removeFromSuperview]; // This is already added to the container
[containerView addSubview:frontView]; // Not yet added ...
}
completion:NULL];
更新
这是我在回答这个问题时写的所有代码。它对我有用。如果它仍然无法为您设置动画,则您可能尚未以相同的方式设置视图层次
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
containerView = [[UIView alloc] initWithFrame:CGRectMake(30, 30, 100, 100)];
[self.view addSubview:containerView];
frontView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 100, 100)];
[frontView setBackgroundColor:[UIColor orangeColor]];
backView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 100, 100)];
[backView setBackgroundColor:[UIColor redColor]];
[containerView addSubview:backView];
UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self
action:@selector(flip)];
[self.view addGestureRecognizer:tap];
}
- (void)flip {
[UIView transitionWithView:containerView
duration:1.0
options:UIViewAnimationOptionTransitionFlipFromLeft
animations:^{
[backView removeFromSuperview];
[containerView addSubview:frontView];
}
completion:NULL];
}
我只是在动画块中尝试了,就像在你的示例中一样,并在旧代码格式中添加到我的问题“仍然不工作它会更改视图,但不会使用翻转,我尝试了更改不透明度和它。”worked@ShimonWiener那代码对我有用。你是按我说的那样设置的吗?包含backView的containerView。containerView添加到超级视图这是我在添加到flipView(容器)的最后3行中单击时使用的功能flipViewFront视图然后我将flipView添加到Main Scroll视图中,然后我在animateFlip中执行动画,我从您的示例中复制了该动画我在问题的末尾添加了函数我尝试了您的代码,它确实有效,我的层次结构很复杂,但是按照顺序构建我认为正如你从我在问题“谢谢你的时间和负担”中提供的代码中看到的那样,找到问题,但不是答案,如果直接从视图加载,并且第一个视图在屏幕上,则一切正常,翻转工作‘如果我更改它,以便用户希望在一个事件中添加视图并翻转它,则显示第二个(翻转)没有翻转的视图我只是在动画块中尝试过,就像在您的示例中一样,并以旧代码格式添加到我的问题“仍然不起作用它会更改视图,但不会使用翻转,我尝试过更改不透明度和透明度。”worked@ShimonWiener那代码对我有用。你是按我说的那样设置的吗?包含backView的containerView。containerView添加到超级视图这是我在添加到flipView(容器)的最后3行中单击时使用的功能flipViewFront视图然后我将flipView添加到Main Scroll视图中,然后我在animateFlip中执行动画,我从您的示例中复制了该动画我在问题的末尾添加了函数我尝试了您的代码,它确实有效,我的层次结构很复杂,但是按照顺序构建我认为正如你从我在问题“谢谢你的时间和负担”中提供的代码中看到的那样,找到问题,但不是答案,如果直接从视图加载,并且第一个视图在屏幕上,则一切正常,翻转工作‘如果我更改它,以便用户希望添加视图并在一个事件中翻转它,则会显示没有翻转的第二个(翻转的)视图