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) {
}];
}
每个视图都有一个覆盖整个视图的透明按钮,因此当用户点击视图时就像点击按钮一样。该按钮调用上面传递发送者的方法。 当动画开始时,所有视图都会翻转,而不仅仅是我从发送者那里获得的视图。 我该怎么办?
i'm developing a game which contained some view (as memory card game) and i want that when the user tap on a card this flip and shows another view. I use this code :
- (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) {
}];
}
Every view has a transparent button which cover the entire view, so when user tap on a view is as tap the button. The button call the method above passing the sender.
When the animation starts all view is flipped, not only the view i get from sender.
How can i do?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
以下代码可能有助于解决您的问题。我认为它比使用透明按钮更干净。
The following code might help with your problem. I think it is cleaner than using a transparent button.
我也有同样的问题。在互联网上搜索不同的帖子后,我想出了一个优雅而简单的解决方案。我将卡片作为自定义 UIButtons。在自定义 UIButton 类中,我添加了使用翻转动画更改背景图像的方法:
希望这对其他人有帮助,因为第一个答案已被接受
更新
如果您位于包含此子的视图上查看代码是:
I had the same problem. After searching different posts on the internet I was able to come up with an elegant and easy solution. I have the cards as custom UIButtons. In the custom UIButton class I added the method which changes the background image with a flip animation:
Hope this helps someone else as the first answer has been already accepted
UPDATE
If you are on the view containing this sub-view the code is:
我在 Swift 4 中的用例:
My use case in Swift 4: