@fiy-fish
2016-08-31T17:30:31.000000Z
字数 2153
阅读 1990
iOS点滴
写的有点乱,解释下,就是在一个类文件里面 比如:
.h
文件中 声明两个类NaviButton
和QuickStartAnnotationView
//
// QuickStartAnnotationView.h
// AMapNaviKit
//
// Created by 刘博 on 16/3/9.
// Copyright © 2016年 AutoNavi. All rights reserved.
//
#import <MAMapKit/MAMapKit.h>
@interface NaviButton : UIButton
@property (nonatomic, strong) UIImageView *carImageView;
@property (nonatomic, strong) UILabel *naviLabel;
@end
@interface QuickStartAnnotationView : MAPinAnnotationView
- (id)initWithAnnotation:(id <MAAnnotation>)annotation reuseIdentifier:(NSString *)reuseIdentifier;
@end
.m
中实现这两个类,而且实现的先后顺序随意
#import "QuickStartAnnotationView.h"
#define naviButtonWidth 44
#define naviButtonHeight 74
@implementation NaviButton
- (instancetype)initWithFrame:(CGRect)frame
{
self = [super initWithFrame:frame];
if (self)
{
[self setBackgroundImage:[UIImage imageNamed:@"naviBackgroundNormal"] forState:UIControlStateNormal];
[self setBackgroundImage:[UIImage imageNamed:@"naviBackgroundHighlighted"] forState:UIControlStateSelected];
//imageView
_carImageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"navi"]];
[self addSubview:_carImageView];
//label
_naviLabel = [[UILabel alloc] init];
_naviLabel.text = @"导航";
_naviLabel.font = [_naviLabel.font fontWithSize:9];
_naviLabel.textColor = [UIColor whiteColor];
[_naviLabel sizeToFit];
[self addSubview:_naviLabel];
}
return self;
}
#define kMarginRatio 0.1
- (void)layoutSubviews
{
[super layoutSubviews];
_carImageView.center = CGPointMake(CGRectGetMidX(self.frame), CGRectGetMidY(self.superview.frame) - CGRectGetHeight(_carImageView.frame) * (0.5 + kMarginRatio));
_naviLabel.center = CGPointMake(CGRectGetMidX(self.frame), CGRectGetMidY(self.superview.frame) + CGRectGetHeight(_naviLabel.frame) * (0.5 + kMarginRatio));
}
@end
@implementation QuickStartAnnotationView
- (id)initWithAnnotation:(id <MAAnnotation>)annotation reuseIdentifier:(NSString *)reuseIdentifier
{
self = [super initWithAnnotation:annotation reuseIdentifier:reuseIdentifier];
if (self)
{
NaviButton *naviButton = [[NaviButton alloc] initWithFrame:(CGRectMake(0, 0, naviButtonWidth, naviButtonHeight))];
self.leftCalloutAccessoryView = naviButton;
}
return self;
}
@end
再啰嗦几句,这个写法的好处,把多个不可分割的类写到一个文件里面
1.减少了文件数,便于查找
2.增强了耦合性,对于可以看成一个整体的部分,可以像这样当做一个整体来写!
3.代码简洁,逻辑清楚。