我正在尝试为 UIButton 实例和 UIImageView 实例之间的翻转效果设置动画。基本上它是一个“翻转扑克牌”效果,一侧(UIImageView)只是一个很好的模式,当翻转时,它应该显示一个带有一些文本的 UIButton。
我的代码存在以下问题:
- UIButton子视图的文字在翻转后不显示
- 翻转动画期间阴影消失
这是目标的直观表示:
任何建议如何解决上述两个问题?
我真的没有想法 - 任何帮助都非常感谢!
这是标题代码:
#import <UIKit/UIKit.h>
@interface CardView : UIControl
@property (nonatomic) BOOL isFrontSide;
- (void)setupView;
- (void)turnCard:(BOOL)inShow withAnimationCompletion:(void (^)(BOOL inFinished))inCompletion;
@end
下面是实现代码:
#import "CardView.h"
#import "UIView+Extension.h"
#import <QuartzCore/QuartzCore.h>
#define kAllControlStates (UIControlStateNormal | UIControlStateHighlighted | UIControlStateDisabled| UIControlStateSelected)
@interface CardView()
@end
@implementation CardView
- (void)setupView {
[self styleViewWithRoundedEdges:YES shadowed:YES];
UIImageView *theBackView = [[UIImageView alloc] initWithFrame:self.bounds];
theBackView.image = [UIImage imageNamed:@"pattern.png"];
theBackView.hidden = NO;
theBackView.userInteractionEnabled = NO;
[theBackView styleViewWithRoundedEdges:YES shadowed:NO];
[self addSubview:theBackView];
UIButton *theFrontView = [[UIButton alloc] initWithFrame:self.bounds];
[theFrontView setTitle:@"Push me !" forState:kAllControlStates];
theFrontView.hidden = YES;
theFrontView.userInteractionEnabled = NO;
[theFrontView styleViewWithRoundedEdges:YES shadowed:NO];
[self addSubview:theFrontView];
}
- (void)turnCard:(BOOL)inShow withAnimationCompletion:(void (^)(BOOL inFinished))inCompletion {
[UIView transitionWithView:self duration:0.75
options:inShow ? UIViewAnimationOptionTransitionFlipFromLeft : UIViewAnimationOptionTransitionFlipFromRight
animations:^{
[(self.subviews)[0] setHidden:inShow]; // UIImage
[(self.subviews)[1] setHidden:!inShow]; // UIButton
}
completion:inCompletion];
}
@end
这是一个在视觉上装饰我的视图的类别:
#import "UIView+Extension.h"
@implementation UIView (Extension)
- (void)styleViewWithRoundedEdges:(BOOL)rounded shadowed:(BOOL)shadowed {
[self styleViewWithRoundedEdges:rounded shadowed:shadowed rasterized:YES];
}
- (void)styleViewWithRoundedEdges:(BOOL)rounded shadowed:(BOOL)shadowed rasterized:(BOOL)rasterized {
if (rounded) {
self.layer.cornerRadius = 3.0;
}
if (shadowed) {
self.layer.shadowColor = [UIColor blackColor].CGColor;
self.layer.shadowOffset = CGSizeMake(2.0, 2.0);
self.layer.shadowOpacity = 0.25;
self.layer.shadowRadius = 1.0;
if(rasterized) {
self.layer.shouldRasterize = YES;
self.layer.rasterizationScale = UIScreen.mainScreen.scale;
}
}
}
@end