CNZFPlayerView.m
4.43 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
//
// ZFPlayerView.m
// ZFPlayer
//
// Copyright (c) 2016年 任子丰 ( http://github.com/renzifeng )
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
// THE SOFTWARE.
#import "CNZFPlayerView.h"
#import "ZFPlayerConst.h"
@implementation CNZFPlayerView
@synthesize presentationSize = _presentationSize;
@synthesize coverImageView = _coverImageView;
- (instancetype)init {
self = [super init];
if (self) {
self.backgroundColor = [UIColor blackColor];
[self addSubview:self.coverImageView];
}
return self;
}
- (void)setPlayerView:(UIView *)playerView {
if (_playerView) {
[_playerView removeFromSuperview];
self.presentationSize = CGSizeZero;
}
_playerView = playerView;
if (playerView != nil) {
[self addSubview:playerView];
}
}
- (void)layoutSubviews {
[super layoutSubviews];
CGFloat min_x = 0;
CGFloat min_y = 0;
CGFloat min_w = 0;
CGFloat min_h = 0;
CGFloat min_view_w = self.frame.size.width;
CGFloat min_view_h = self.frame.size.height;
CGSize playerViewSize = CGSizeZero;
CGFloat videoWidth = self.presentationSize.width;
CGFloat videoHeight = self.presentationSize.height;
if (videoHeight == 0) return;
CGFloat screenScale = min_view_w/min_view_h;
CGFloat videoScale = videoWidth/videoHeight;
if (screenScale > videoScale) {
CGFloat height = min_view_h;
CGFloat width = height * videoScale;
playerViewSize = CGSizeMake(width, height);
} else {
CGFloat width = min_view_w;
CGFloat height = width / videoScale;
playerViewSize = CGSizeMake(width, height);
}
if (self.scalingMode == ZFPlayerScalingModeNone || self.scalingMode == ZFPlayerScalingModeAspectFit) {
min_w = playerViewSize.width;
min_h = playerViewSize.height;
min_x = (min_view_w - min_w) / 2.0;
min_y = (min_view_h - min_h) / 2.0;
self.playerView.frame = CGRectMake(min_x, min_y, min_w, min_h);
} else if (self.scalingMode == ZFPlayerScalingModeAspectFill || self.scalingMode == ZFPlayerScalingModeFill) {
self.playerView.frame = self.bounds;
}
self.coverImageView.frame = self.playerView.frame;
}
- (CGSize)presentationSize {
if (CGSizeEqualToSize(_presentationSize, CGSizeZero)) {
_presentationSize = self.frame.size;
}
return _presentationSize;
}
- (UIImageView *)coverImageView {
if (!_coverImageView) {
_coverImageView = [[UIImageView alloc] init];
_coverImageView.userInteractionEnabled = YES;
_coverImageView.clipsToBounds = YES;
_coverImageView.contentMode = UIViewContentModeScaleAspectFit;
}
return _coverImageView;
}
- (void)setScalingMode:(ZFPlayerScalingMode)scalingMode {
_scalingMode = scalingMode;
if (scalingMode == ZFPlayerScalingModeNone || scalingMode == ZFPlayerScalingModeAspectFit) {
self.coverImageView.contentMode = UIViewContentModeScaleAspectFit;
} else if (scalingMode == ZFPlayerScalingModeAspectFill) {
self.coverImageView.contentMode = UIViewContentModeScaleAspectFill;
} else if (scalingMode == ZFPlayerScalingModeFill) {
self.coverImageView.contentMode = UIViewContentModeScaleToFill;
}
[self layoutIfNeeded];
}
- (void)setPresentationSize:(CGSize)presentationSize {
_presentationSize = presentationSize;
if (CGSizeEqualToSize(CGSizeZero, presentationSize)) return;
[self setNeedsLayout];
[self layoutIfNeeded];
}
@end