And*_*ino 28 animation flip uiview ios
我正在开发一个包含一些视图的游戏(作为存储卡游戏),我希望当用户点击卡片时这个翻转并显示另一个视图.我用这个代码:
- (void)flipCard:(id)sender {
UIButton *btn=(UIButton *)sender;
UIView *view=[btn superview];
UIView *flipView=[[UIView alloc] initWithFrame:[view frame]];
[flipView setBackgroundColor:[UIColor blueColor]];
[[flipView layer] setCornerRadius:10];
NSLog(@"Flip card : view frame = %f, %f",view.frame.origin.x, view.frame.origin.y);
[UIView transitionFromView:view toView:flipView duration:1.0 options:UIViewAnimationOptionTransitionFlipFromLeft completion:^(BOOL finished) {
}];
}
Run Code Online (Sandbox Code Playgroud)
每个视图都有一个覆盖整个视图的透明按钮,因此当用户点击视图时点击按钮.按钮调用上面传递发送方的方法.当动画开始时,所有视图都被翻转,不仅是我从发送者那里获得的视图.我能怎么做?
car*_*los 46
以下代码可能有助于解决您的问题.我认为它比使用透明按钮更清洁.
- (void)viewDidLoad {
[super viewDidLoad];
flipped = NO;
UITapGestureRecognizer *tapGesture = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(handleTap:)];
[flipContainerView addGestureRecognizer:tapGesture];
[tapGesture release];
}
- (void)handleTap:(UITapGestureRecognizer *)sender {
if (sender.state == UIGestureRecognizerStateEnded) {
[UIView transitionWithView:flipContainerView
duration:1
options:UIViewAnimationOptionTransitionFlipFromLeft
animations:^{
if (!flipped) {
[frontCard setHidden:YES];
[flipContainerView addSubview:backCard.view]; //or unhide it.
flipped = YES;
} else {
[frontCard setHidden:NO];
[backCard removeFromSuperview]; //or hide it.
}
} completion:nil];
}
}
Run Code Online (Sandbox Code Playgroud)
San*_*rea 13
我有同样的问题.在互联网上搜索不同的帖子后,我能够提出一个优雅而简单的解决方案.我有卡作为自定义UIButtons.在自定义UIButton类中,我添加了使用翻转动画更改背景图像的方法:
-(void) flipCard{
[UIView transitionWithView:self
duration:0.3f
options:UIViewAnimationOptionTransitionFlipFromRight|UIViewAnimationOptionCurveEaseInOut
animations:^{
if (self.isFlipped) {
[self setBackgroundImage:[UIImage imageNamed:@"card_back_2.png"] forState:UIControlStateNormal];
}else{
[self setBackgroundImage:[UIImage imageNamed:self.cardName] forState:UIControlStateNormal];
}
} completion:NULL];
self.isFlipped = !self.isFlipped;
}
Run Code Online (Sandbox Code Playgroud)
希望这有助于其他人,因为第一个答案已被接受
UPDATE
如果您在包含此子视图的视图中,则代码为:
-(void)flipCard:(APCard*)card{
[UIView transitionWithView:card
duration:kFlipTime
options:UIViewAnimationOptionTransitionFlipFromRight|UIViewAnimationOptionCurveEaseInOut
animations:^{
if (card.isFlipped) {
[card setBackgroundImage:[UIImage imageNamed:@"card_back_2.png"] forState:UIControlStateNormal];
}else{
[card setBackgroundImage:[UIImage imageNamed:card.cardName] forState:UIControlStateNormal];
}
completion:^(BOOL finished) {
if (finished) {
//DO Stuff
}
}
];
card.isFlipped = !card.isFlipped;
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
27885 次 |
| 最近记录: |