图一
图二
第一张图内的代码虽然功能也能实现,但是当这个SwitchListView第一次被父容器调起的时候,动画效果并不是UIView animation内所写的效果。然后被调起过一次之后却又一切正常。(尚还不知道为什么)几番尝试后改成第二张图内的代码就好了。
贴一下最后的代码吧:
#import “SwitchListView.h”
static SwitchListView *instance;
@interface SwitchListView ()
@property (weak, nonatomic) IBOutlet NSLayoutConstraint *width;
@property (weak, nonatomic) IBOutlet NSLayoutConstraint *height;
@property (weak, nonatomic) IBOutlet UILabel *mapLable;
@property (weak, nonatomic) IBOutlet UILabel *textLable;
@end
@implementation SwitchListView
(SwitchListView )startWithView:(UIView )superView{
instance = [[SwitchListView alloc ]initWithFrame:superView.bounds];
[UIView animateWithDuration:0.1 animations:^{
[superView addSubview:instance];
}completion:^(BOOL finished) {
[UIView animateWithDuration:0.3 animations:^{
instance.searchTopSpe.constant = 10.;
[instance layoutIfNeeded];
}completion:^(BOOL finished) {}];
}];
return instance;
}(instancetype)initWithFrame:(CGRect)frame{
if (!instance) {
self = [super initWithFrame:frame];
instance = [[NSBundle mainBundle] loadNibNamed:@”SwitchListView” owner:nil options:nil][0];
instance.frame = frame;
}
return instance;
}- (void)layoutSubviews{
[super layoutSubviews];
self.mapLable.text = LS(@”map search”);
self.textLable.text = LS(@”text search”);
}
//地图搜索 - (IBAction)mapSearchClick:(id)sender {
if ([self.delegate respondsToSelector:@selector(seletedSearchType:)]) {
[self.delegate seletedSearchType:1];
}
[self stop];
}
//文本搜索 - (IBAction)textSearchClick:(id)sender {
if ([self.delegate respondsToSelector:@selector(seletedSearchType:)]) {
[self.delegate seletedSearchType:2];
}
[self stop];
} (IBAction)tap_action:(id)sender {
[self stop];
}(void)stop{
[UIView animateWithDuration:0.3 animations:^{
self.searchTopSpe.constant = -70.;
[self layoutIfNeeded];
} completion:^(BOOL finished) {
[self removeFromSuperview];
}];
}
@end