[关闭]
@fiy-fish 2016-08-31T17:30:31.000000Z 字数 2153 阅读 1990

多个类写到一个类文件里面(高德官方demo源码总结)

iOS点滴


可以在一个类里面写多个 类 类的实现部分的顺序可以不一致

  1. //
  2. // QuickStartAnnotationView.h
  3. // AMapNaviKit
  4. //
  5. // Created by 刘博 on 16/3/9.
  6. // Copyright © 2016年 AutoNavi. All rights reserved.
  7. //
  8. #import <MAMapKit/MAMapKit.h>
  9. @interface NaviButton : UIButton
  10. @property (nonatomic, strong) UIImageView *carImageView;
  11. @property (nonatomic, strong) UILabel *naviLabel;
  12. @end
  13. @interface QuickStartAnnotationView : MAPinAnnotationView
  14. - (id)initWithAnnotation:(id <MAAnnotation>)annotation reuseIdentifier:(NSString *)reuseIdentifier;
  15. @end
  1. #import "QuickStartAnnotationView.h"
  2. #define naviButtonWidth 44
  3. #define naviButtonHeight 74
  4. @implementation NaviButton
  5. - (instancetype)initWithFrame:(CGRect)frame
  6. {
  7. self = [super initWithFrame:frame];
  8. if (self)
  9. {
  10. [self setBackgroundImage:[UIImage imageNamed:@"naviBackgroundNormal"] forState:UIControlStateNormal];
  11. [self setBackgroundImage:[UIImage imageNamed:@"naviBackgroundHighlighted"] forState:UIControlStateSelected];
  12. //imageView
  13. _carImageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"navi"]];
  14. [self addSubview:_carImageView];
  15. //label
  16. _naviLabel = [[UILabel alloc] init];
  17. _naviLabel.text = @"导航";
  18. _naviLabel.font = [_naviLabel.font fontWithSize:9];
  19. _naviLabel.textColor = [UIColor whiteColor];
  20. [_naviLabel sizeToFit];
  21. [self addSubview:_naviLabel];
  22. }
  23. return self;
  24. }
  25. #define kMarginRatio 0.1
  26. - (void)layoutSubviews
  27. {
  28. [super layoutSubviews];
  29. _carImageView.center = CGPointMake(CGRectGetMidX(self.frame), CGRectGetMidY(self.superview.frame) - CGRectGetHeight(_carImageView.frame) * (0.5 + kMarginRatio));
  30. _naviLabel.center = CGPointMake(CGRectGetMidX(self.frame), CGRectGetMidY(self.superview.frame) + CGRectGetHeight(_naviLabel.frame) * (0.5 + kMarginRatio));
  31. }
  32. @end
  33. @implementation QuickStartAnnotationView
  34. - (id)initWithAnnotation:(id <MAAnnotation>)annotation reuseIdentifier:(NSString *)reuseIdentifier
  35. {
  36. self = [super initWithAnnotation:annotation reuseIdentifier:reuseIdentifier];
  37. if (self)
  38. {
  39. NaviButton *naviButton = [[NaviButton alloc] initWithFrame:(CGRectMake(0, 0, naviButtonWidth, naviButtonHeight))];
  40. self.leftCalloutAccessoryView = naviButton;
  41. }
  42. return self;
  43. }
  44. @end

再啰嗦几句,这个写法的好处,把多个不可分割的类写到一个文件里面
1.减少了文件数,便于查找
2.增强了耦合性,对于可以看成一个整体的部分,可以像这样当做一个整体来写!
3.代码简洁,逻辑清楚。

添加新批注
在作者公开此批注前,只有你和作者可见。
回复批注