| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408 |
- //
- // TCVideoRangeContent.m
- // SAVideoRangeSliderExample
- //
- // Created by annidyfeng on 2017/4/18.
- // Copyright © 2017年 Andrei Solovjev. All rights reserved.
- //
- #import "TCRangeContent.h"
- //#import "UIView+AdditionsX12.h"
- #import "TCVideoRangeConst.h"
- @implementation TCRangeContentConfig
- - (id)init
- {
- if (self = [super init]) {
- _pinWidth = PIN_WIDTH;
- _thumbHeight = THUMB_HEIGHT;
- _borderHeight = BORDER_HEIGHT;
- _leftPinImage = [UIImage imageNamed:@"left"];
- _rightPigImage = [UIImage imageNamed:@"right"];
- //_centerPinImage = [UIImage imageNamed:@"center"];
- //10-15
- _centerPinImage = [UIImage imageNamed:@"滑块-中部"];
- }
-
- return self;
- }
- @end
- @interface TCRangeContent()
- @end
- @implementation TCRangeContent {
- CGFloat _imageWidth;
- TCRangeContentConfig* _appearanceConfig;
- }
- - (instancetype)initWithImageList:(NSArray *)images
- {
- _imageList = images;
- _appearanceConfig = [TCRangeContentConfig new];
-
- CGRect frame = {.origin = CGPointZero, .size = [self intrinsicContentSize]};
- self = [super initWithFrame:frame];
-
- [self iniSubViews];
-
- return self;
- }
- - (instancetype)initWithImageList:(NSArray *)images config:(TCRangeContentConfig *)config
- {
- _imageList = images;
- _appearanceConfig = config;
-
- CGRect frame = {.origin = CGPointZero, .size = [self intrinsicContentSize]};
- self = [super initWithFrame:frame];
-
- [self iniSubViews];
-
- return self;
- }
- - (void)iniSubViews
- {
- CGRect frame = self.bounds;
- NSMutableArray *tmpList = [NSMutableArray new];
- for (int i = 0; i < _imageList.count; i++) {
- CGRect imgFrame = CGRectMake(_appearanceConfig.pinWidth + i*[self imageWidth],
- _appearanceConfig.borderHeight,
- [self imageWidth],
- _appearanceConfig.thumbHeight);
- UIImageView *imgView = [[UIImageView alloc] initWithFrame:imgFrame];
- imgView.image = _imageList[i];
- imgView.contentMode = (_imageList.count > 1 ? UIViewContentModeScaleToFill : UIViewContentModeScaleAspectFit);
- [self addSubview:imgView];
- [tmpList addObject:imgView];
- }
- _imageViewList = tmpList;
-
- // self.centerCover = ({
- // UIView *view = [[UIView alloc] initWithFrame:CGRectZero];
- // [self addSubview:view];
- // view.userInteractionEnabled = YES;
- // UIPanGestureRecognizer *panGes = [[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(handleCenterPan:)];
- // [view addGestureRecognizer:panGes];
- // view.accessibilityIdentifier = @"center";
- // view;
- // });
-
- if (_appearanceConfig.leftCorverImage) {
- self.leftCover = [[UIImageView alloc] initWithImage:_appearanceConfig.leftCorverImage];
- self.leftCover.contentMode = UIViewContentModeCenter;
- self.leftCover.clipsToBounds = YES;
- self.leftCover.layer.masksToBounds = YES;
- self.leftCover.layer.cornerRadius = 5;
-
- }
- else {
- self.leftCover = [[UIImageView alloc] initWithFrame:CGRectZero];
- self.leftCover.backgroundColor = [UIColor blackColor];
- self.leftCover.alpha = 0.5;
- self.leftCover.layer.masksToBounds = YES;
- self.leftCover.layer.cornerRadius = 5;
- };
- [self addSubview:self.leftCover];
-
-
- if (_appearanceConfig.rightCoverImage) {
- self.rightCover = [[UIImageView alloc] initWithImage:_appearanceConfig.rightCoverImage];
- self.rightCover.contentMode = UIViewContentModeCenter;
- self.rightCover.clipsToBounds = YES;
- self.rightCover.layer.masksToBounds = YES;
- self.rightCover.layer.cornerRadius = 5;
- }
- else {
- self.rightCover = [[UIImageView alloc] initWithFrame:CGRectZero];
- self.rightCover.backgroundColor = [UIColor blackColor];
- self.rightCover.alpha = 0.5;
- self.rightCover.layer.masksToBounds = YES;
- self.rightCover.layer.cornerRadius = 5;
- }
- [self addSubview:self.rightCover];
-
- self.leftPin = ({
- UIImageView *imageView = [[UIImageView alloc] initWithImage:_appearanceConfig.leftPinImage];
- imageView.contentMode = UIViewContentModeScaleToFill;
- imageView.width = _appearanceConfig.pinWidth;
- [self addSubview:imageView];
- imageView.userInteractionEnabled = YES;
- UIPanGestureRecognizer *panGes = [[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(handleLeftPan:)];
- [imageView addGestureRecognizer:panGes];
- imageView;
- });
-
- self.centerPin = ({
- UIImageView *imageView = [[UIImageView alloc] initWithImage:_appearanceConfig.centerPinImage];
- /*
- imageView.contentMode = UIViewContentModeScaleToFill;
- imageView.width = _appearanceConfig.pinWidth;
- */
- //10-15
- imageView.contentMode = UIViewContentModeScaleToFill;
- imageView.width = _appearanceConfig.pinWidth*1.5;
-
- [self addSubview:imageView];
- imageView.userInteractionEnabled = YES;
- UIPanGestureRecognizer *panGes = [[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(handleCenterPan:)];
- [imageView addGestureRecognizer:panGes];
- imageView;
- });
- self.centerPin.hidden = YES;
-
- self.rightPin = ({
- UIImageView *imageView = [[UIImageView alloc] initWithImage:_appearanceConfig.rightPigImage];
- imageView.contentMode = UIViewContentModeScaleToFill;
- imageView.width = _appearanceConfig.pinWidth;
- [self addSubview:imageView];
- imageView.userInteractionEnabled = YES;
- UIPanGestureRecognizer *panGes = [[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(handleRightPan:)];
- [imageView addGestureRecognizer:panGes];
- imageView;
- });
-
- self.topBorder = ({
- UIView *view = [[UIView alloc] initWithFrame:CGRectZero];
- [self addSubview:view];
- // view.backgroundColor = [UIColor colorWithRed:0.14 green:0.80 blue:0.67 alpha:1];
- view.backgroundColor = Pink_Cor;
- view;
- });
-
- self.bottomBorder = ({
- UIView *view = [[UIView alloc] initWithFrame:CGRectZero];
- [self addSubview:view];
- // view.backgroundColor = [UIColor colorWithRed:0.14 green:0.80 blue:0.67 alpha:1];
- view.backgroundColor = Pink_Cor;
- view;
- });
-
- _leftPinCenterX = _appearanceConfig.pinWidth / 2;
- _centerPinCenterX = frame.size.width / 2;
- _rightPinCenterX = frame.size.width- _appearanceConfig.pinWidth / 2;
- }
- - (CGSize)intrinsicContentSize {
- return CGSizeMake([self imageWidth] * self.imageList.count + 2 * _appearanceConfig.pinWidth, _appearanceConfig.thumbHeight + 2 * _appearanceConfig.borderHeight);
- }
- - (void)layoutSubviews {
- [super layoutSubviews];
-
- self.leftPin.center = CGPointMake(self.leftPinCenterX, self.height / 2);
- self.centerPin.center = CGPointMake(self.centerPinCenterX, self.height / 2);
- self.rightPin.center = CGPointMake(self.rightPinCenterX, self.height / 2);
-
- self.topBorder.height = _appearanceConfig.borderHeight;
- self.topBorder.width = self.rightPinCenterX - self.leftPinCenterX;
- self.topBorder.y = self.leftPin.top;//0;
- self.topBorder.x = self.leftPinCenterX;
-
- self.bottomBorder.height = _appearanceConfig.borderHeight;
- self.bottomBorder.width = self.rightPinCenterX - self.leftPinCenterX;
- self.bottomBorder.y = self.leftPin.bottom-_appearanceConfig.borderHeight;
- self.bottomBorder.x = self.leftPinCenterX;
-
- self.centerCover.height = _appearanceConfig.thumbHeight - 2 * _appearanceConfig.borderHeight;
- self.centerCover.width = self.rightPinCenterX - self.leftPinCenterX - _appearanceConfig.pinWidth;
- self.centerCover.y = _appearanceConfig.borderHeight;
- self.centerCover.x = self.leftPinCenterX + _appearanceConfig.pinWidth / 2;
-
- self.leftCover.height = _appearanceConfig.thumbHeight;
- self.leftCover.width = self.leftPinCenterX;
- self.leftCover.y = _appearanceConfig.borderHeight;
- self.leftCover.x = 0;
-
- self.rightCover.height = _appearanceConfig.thumbHeight;
- self.rightCover.width = self.width - self.rightPinCenterX;
- self.rightCover.y = _appearanceConfig.borderHeight;
- self.rightCover.x = self.rightPinCenterX;
- }
- #pragma mark - Gestures
- - (void)handleLeftPan:(UIPanGestureRecognizer *)gesture
- {
- if (gesture.state == UIGestureRecognizerStateBegan || gesture.state == UIGestureRecognizerStateChanged || gesture.state == UIGestureRecognizerStateEnded) {
-
- CGPoint translation = [gesture translationInView:self];
-
- _leftPinCenterX += translation.x;
- if (_leftPinCenterX < _appearanceConfig.pinWidth / 2) {
- _leftPinCenterX = _appearanceConfig.pinWidth / 2;
- }
-
- if (_centerPin.isHidden){
- if (_rightPinCenterX - _leftPinCenterX <= _appearanceConfig.pinWidth) {
- _leftPinCenterX = _rightPinCenterX - _appearanceConfig.pinWidth;
- }
- }else{
- if (_centerPinCenterX - _leftPinCenterX <= _appearanceConfig.pinWidth) {
- _leftPinCenterX = _centerPinCenterX - _appearanceConfig.pinWidth;
- }
- }
-
- [gesture setTranslation:CGPointZero inView:self];
-
- [self setNeedsLayout];
-
- if (gesture.state == UIGestureRecognizerStateBegan) {
- if ([self.delegate respondsToSelector:@selector(onRangeLeftChangeBegin:)])
- [self.delegate onRangeLeftChangeBegin:self];
- }
- else if (gesture.state == UIGestureRecognizerStateChanged){
- if ([self.delegate respondsToSelector:@selector(onRangeLeftChanged:)])
- [self.delegate onRangeLeftChanged:self];
- }
- else {
- if ([self.delegate respondsToSelector:@selector(onRangeLeftChangeEnded:)])
- [self.delegate onRangeLeftChangeEnded:self];
- }
-
- }
- }
- - (void)handleCenterPan:(UIPanGestureRecognizer *)gesture
- {
- if (gesture.state == UIGestureRecognizerStateBegan || gesture.state == UIGestureRecognizerStateChanged || gesture.state == UIGestureRecognizerStateEnded) {
-
- CGPoint translation = [gesture translationInView:self];
-
- _centerPinCenterX += translation.x;
- if (_centerPinCenterX < _leftPinCenterX + _appearanceConfig.pinWidth) {
- _centerPinCenterX = _leftPinCenterX + _appearanceConfig.pinWidth;
- }
- if (_centerPinCenterX > _rightPinCenterX - _appearanceConfig.pinWidth) {
- _centerPinCenterX = _rightPinCenterX - _appearanceConfig.pinWidth;
- }
-
- [gesture setTranslation:CGPointZero inView:self];
-
- [self setNeedsLayout];
-
- if (gesture.state == UIGestureRecognizerStateBegan) {
- if ([self.delegate respondsToSelector:@selector(onRangeCenterChangeBegin:)])
- [self.delegate onRangeCenterChangeBegin:self];
- }
- else if (gesture.state == UIGestureRecognizerStateChanged){
- if ([self.delegate respondsToSelector:@selector(onRangeCenterChanged:)])
- [self.delegate onRangeCenterChanged:self];
- }
- else {
- if ([self.delegate respondsToSelector:@selector(onRangeCenterChangeEnded:)])
- [self.delegate onRangeCenterChangeEnded:self];
- }
-
- }
- }
- - (void)handleRightPan:(UIPanGestureRecognizer *)gesture
- {
- if (gesture.state == UIGestureRecognizerStateBegan || gesture.state == UIGestureRecognizerStateChanged || gesture.state == UIGestureRecognizerStateEnded) {
-
- CGPoint translation = [gesture translationInView:self];
-
- _rightPinCenterX += translation.x;
- if (_rightPinCenterX > self.width - _appearanceConfig.pinWidth / 2) {
- _rightPinCenterX = self.width - _appearanceConfig.pinWidth / 2;
- }
-
- if (_centerPin.isHidden) {
- if (_rightPinCenterX-_leftPinCenterX <= _appearanceConfig.pinWidth) {
- _rightPinCenterX = _leftPinCenterX + _appearanceConfig.pinWidth;
- }
- }else{
- if (_rightPinCenterX-_centerPinCenterX <= _appearanceConfig.pinWidth) {
- _rightPinCenterX = _centerPinCenterX + _appearanceConfig.pinWidth;
- }
- }
-
- [gesture setTranslation:CGPointZero inView:self];
-
- [self setNeedsLayout];
-
-
- if (gesture.state == UIGestureRecognizerStateBegan) {
- if ([self.delegate respondsToSelector:@selector(onRangeRightChangeBegin:)])
- [self.delegate onRangeRightChangeBegin:self];
- }
- else if (gesture.state == UIGestureRecognizerStateChanged) {
- if ([self.delegate respondsToSelector:@selector(onRangeRightChanged:)])
- [self.delegate onRangeRightChanged:self];
- }
- else {
- if ([self.delegate respondsToSelector:@selector(onRangeRightChangeEnded:)])
- [self.delegate onRangeRightChangeEnded:self];
- }
- }
- }
- //- (void)handleCenterPan:(UIPanGestureRecognizer *)gesture
- //{
- //
- // if (gesture.state == UIGestureRecognizerStateBegan || gesture.state == UIGestureRecognizerStateChanged) {
- //
- // CGPoint translation = [gesture translationInView:self];
- //
- // _leftPinCenterX += translation.x;
- // _rightPinCenterX += translation.x;
- //
- // if (_rightPinCenterX > self.width - _appearanceConfig.pinWidth || _leftPinCenterX < _appearanceConfig.pinWidth / 2){
- // _leftPinCenterX -= translation.x;
- // _rightPinCenterX -= translation.x;
- // }
- //
- // [gesture setTranslation:CGPointZero inView:self];
- //
- // [self setNeedsLayout];
- //
- // if ([self.delegate respondsToSelector:@selector(onRangeLeftAndRightChanged:)])
- // [self.delegate onRangeLeftAndRightChanged:self];
- //
- // }
- //}
- - (CGFloat)pinWidth
- {
- return _appearanceConfig.pinWidth;
- }
- - (CGFloat)imageWidth
- {
- UIImage *img = self.imageList[0];
- if (self.imageList.count == 1) {
- return MIN(img.size.width, [UIScreen mainScreen].bounds.size.width - 2 * _appearanceConfig.pinWidth);
- }
- _imageWidth = img.size.width/img.size.height*_appearanceConfig.thumbHeight;
- return _imageWidth;
- }
- - (CGFloat)imageListWidth {
- return self.imageList.count * [self imageWidth];
- }
- - (CGFloat)leftScale {
- CGFloat imagesLength = [self imageWidth] * self.imageViewList.count;
- return MAX(0, (_leftPinCenterX - _appearanceConfig.pinWidth / 2) / imagesLength);
- }
- - (CGFloat)rightScale {
- CGFloat imagesLength = [self imageWidth] * self.imageViewList.count;
- return MAX(0, (_rightPinCenterX - _appearanceConfig.pinWidth / 2 - _appearanceConfig.pinWidth) / imagesLength);
- }
- - (CGFloat)centerScale {
- CGFloat imagesLength = [self imageWidth] * self.imageViewList.count;
- return MAX(0, (_centerPinCenterX - _appearanceConfig.pinWidth / 2 - _appearanceConfig.pinWidth) / imagesLength);
- }
- @end
|