iOS开发 - 类似驾考宝典题目切换控件实现

IOS 类似驾考宝典题目切换控件实现

当我们题目切换控件时候,很多时候我们会看下驾考宝典的效果

具体如何实现该效果呢,手势控制及切换view
下面给出具体代码如下

  • 1、内容控件UIView。JJDQContentView.h

#import "BaseView.h"

@interface JJDQContentView : BaseView

@property (nonatomic, strong) NSString *title;
@property (nonatomic, strong) NSString *content;

@property (nonatomic, assign) NSInteger contentTag;

@end

  • 2、内容控件UIView。JJDQContentView.m

#import "JJDQContentView.h"

@interface JJDQContentView ()

@property (nonatomic, strong) UILabel *titleLabel;
@property (nonatomic, strong) UILabel *contentLabel;

@end

@implementation JJDQContentView

- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        
        _titleLabel = [[UILabel alloc] initWithFrame:CGRectZero];
        _titleLabel.backgroundColor = [UIColor clearColor];
        _titleLabel.textAlignment = NSTextAlignmentCenter;
        _titleLabel.font = [UIFont systemFontOfSize:16];
        _titleLabel.textColor = [UIColor colorWithHexString:@"555555"];
        [self addSubview:_titleLabel];
        
        _contentLabel = [[UILabel alloc] initWithFrame:CGRectZero];
        _contentLabel.backgroundColor = [UIColor clearColor];
        _contentLabel.textAlignment = NSTextAlignmentCenter;
        _contentLabel.font = [UIFont systemFontOfSize:14];
        _contentLabel.textColor = [UIColor colorWithHexString:@"7a7a7a"];
        _contentLabel.numberOfLines = 0;
        _contentLabel.lineBreakMode = NSLineBreakByCharWrapping;
        [self addSubview:_contentLabel];
    }
    return self;
}

- (void)layoutSubviews {
    [super layoutSubviews];
    
    self.titleLabel.frame = CGRectMake(30.0, 50.0, CGRectGetWidth(self.bounds) - 60.0, 50.0);

    self.contentLabel.frame = CGRectMake(30.0, 0.0, CGRectGetWidth(self.bounds) - 60.0, CGRectGetHeight(self.bounds) - CGRectGetMaxY(self.titleLabel.frame));
}

- (void)setTitle:(NSString *)title {
    _title = title;
    self.titleLabel.text = [NSString stringWithFormat:@"%@",title];
    [self setNeedsLayout];
}

- (void)setContent:(NSString *)content {
    _content = content;
    self.contentLabel.text = [NSString stringWithFormat:@"%@",content];
    [self setNeedsLayout];
}

- (void)dealloc {
        
}

@end

  • 3、实现切换及手势控制的UIView。JJDQSwitchView.h
#import "JJDBaseView.h"

@interface JJDQSwitchView : JJDBaseView

@end

  • 4、实现切换及手势控制的UIView。JJDQSwitchView.m
#import "JJDQSwitchView.h"
#import "JJDQContentView.h"

@interface JJDQSwitchView ()

@property (nonatomic, strong) JJDQContentView *leftView;
@property (nonatomic, strong) JJDQContentView *centerView;
@property (nonatomic, strong) JJDQContentView *rightView;

@property (nonatomic, assign) CGPoint startPoint;
@property (nonatomic, assign) NSInteger contentTag;

@property (nonatomic, strong) NSMutableArray *qLists;

@end

@implementation JJDQSwitchView

- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        self.contentTag = 0;
        for (int i = 0; i < 3; i ++) {
            JJDQContentView *view = [[JJDQContentView alloc] initWithFrame:CGRectMake((i - 1) * kScreenWidth, self.navigationHeight, kScreenWidth, kScreenHeight)];
            view.backgroundColor = [UIColor colorWithHexString:@"eff1f4"];
            [self addSubview:view];
            switch (i) {
                case 0:
                    self.leftView = view;
                    view.contentTag = -1;
                    break;
                case 1:
                    self.centerView = view;
                    view.contentTag = 0;
                    view.title = [NSString stringWithFormat:@"第%ld页",(long)self.contentTag];
                    break;
                case 2:
                    self.rightView = view;
                    view.contentTag = 1;
                    view.title = [NSString stringWithFormat:@"第%ld页",(long)self.contentTag + 1];
                    break;
                default:
                    break;
            }
        }
        [self sendSubviewToBack:self.centerView];
        [self addGestureRecognizer:[[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(moveView:)]];
        
        [self bringSubviewToFront:self.navigationBar];
    }
    return self;
}

- (void)layoutSubviews {
    [super layoutSubviews];
}

-(void)moveView:(UIPanGestureRecognizer *)pan{
    CGPoint point = [pan locationInView:self];
    switch (pan.state) {
        case UIGestureRecognizerStateBegan:
            self.startPoint = point;
            break;
        case UIGestureRecognizerStateChanged:{
            CGFloat lenth = point.x - self.startPoint.x;
            if (lenth > 0) {
                if (self.contentTag == 0) {
                    return;
                }
                
                CGRect frame = self.leftView.frame;
                frame.origin.x = -kScreenWidth + lenth;
                self.leftView.frame = frame;
            }else{
                CGRect frame = self.rightView.frame;
                frame.origin.x = kScreenWidth + lenth;
                self.rightView.frame = frame;
            }
        }
            
            break;
        case UIGestureRecognizerStateEnded:{
            CGFloat lenth = point.x - self.startPoint.x;
            if (lenth > 0 ) {
                if (self.contentTag == 0) {
                    return;
                }
                if (lenth > kScreenWidth/3) {
                    [UIView animateWithDuration:0.3 animations:^{
                        
                        CGRect frame = self.leftView.frame;
                        frame.origin.x = 0;
                        self.leftView.frame = frame;
                        
                    } completion:^(BOOL finished) {
                        self.contentTag -= 1;
                        
                        CGRect frame = self.centerView.frame;
                        frame.origin.x = -kScreenWidth;
                        self.centerView.frame = frame;
                        
                        JJDQContentView *view = self.leftView;
                        self.leftView = self.centerView;
                        self.centerView = view;
                        [self sendSubviewToBack:self.centerView];
                        
                        self.leftView.contentTag -= 2;
                        self.leftView.title = [NSString stringWithFormat:@"第%ld题",(long)self.leftView.contentTag];
                        self.leftView.content = [self randomTitle];
                        
                        self.rightView.contentTag -= 1;
                        self.rightView.title = [NSString stringWithFormat:@"第%ld题",(long)self.rightView.contentTag];
                        self.rightView.content = [self randomTitle];
                    }];
                }else{
                    [UIView animateWithDuration:0.3 animations:^{
                        
                        CGRect frame = self.leftView.frame;
                        frame.origin.x = -kScreenWidth;
                        self.leftView.frame = frame;
                    }];
                }
            }else{
                lenth = -lenth;
                if (lenth > kScreenWidth/3) {
                    [UIView animateWithDuration:0.3 animations:^{

                        CGRect frame = self.rightView.frame;
                        frame.origin.x = 0.0;
                        self.rightView.frame = frame;

                    } completion:^(BOOL finished) {
                        self.contentTag += 1;
                        
                        CGRect frame = self.centerView.frame;
                        frame.origin.x = kScreenWidth;
                        self.centerView.frame = frame;
                        
                        JJDQContentView * view = self.rightView;
                        self.rightView = self.centerView;
                        self.centerView = view;
                        [self sendSubviewToBack:self.centerView];
                        self.rightView.contentTag += 2;
                        self.rightView.title = [NSString stringWithFormat:@"第%ld题目",(long)self.rightView.contentTag];
                        self.rightView.content = [self randomTitle];
                        
                        self.leftView.contentTag += 1;
                        self.leftView.title = [NSString stringWithFormat:@"第%ld题",(long)self.leftView.contentTag];
                        self.leftView.content = [self randomTitle];
                        
                    }];
                }else{
                    [UIView animateWithDuration:0.3 animations:^{
                        CGRect frame = self.rightView.frame;
                        frame.origin.x = kScreenWidth;
                        self.rightView.frame = frame;

                    }];
                }
            }
        }
            break;
        default:
            break;
    }
}


- (JJDQContentView *)leftView {
    if (!_leftView) {
        _leftView = [[JJDQContentView alloc] init];
    }
    return _leftView;
}
- (JJDQContentView *)centerView {
    if (!_centerView) {
        _centerView = [[JJDQContentView alloc] init];
    }
    return _centerView;
}
- (JJDQContentView *)rightView {
    if (!_rightView) {
        _rightView = [[JJDQContentView alloc] init];
    }
    return _rightView;
}

- (NSMutableArray *)qLists{
    if (!_qLists) {
        _qLists = [NSMutableArray arrayWithObjects:@"你可以从别人那里得来思想,你的思想方法,即熔铸思想的模子却必须是你自己的。你可以从别人那里得来思想,你的思想方法,即熔铸思想的模子却必须是你自己的。你可以从别人那里得来思想,你的思想方法,即熔铸思想的模子却必须是你自己的。",@"按照自己的意志去做,不要听那些闲言碎语,你就一定会成功。按照自己的意志去做,不要听那些闲言碎语,你就一定会成功。按照自己的意志去做,不要听那些闲言碎语,你就一定会成功。按照自己的意志去做,不要听那些闲言碎语,你就一定会成功。",@"成功的秘诀,在永不改变既定的目的。成功的秘诀,在永不改变既定的目的。成功的秘诀,在永不改变既定的目的。成功的秘诀,在永不改变既定的目的。成功的秘诀,在永不改变既定的目的。", nil];
    }
    return _qLists;
}

- (NSString *)randomTitle {
    int index = (arc4random() % self.qLists.count);
    NSString *content = [self.qLists objectAtIndex:index];
    return content;
}

- (void)dealloc {
    
}

@end


本文为学习的记录,以便之后查阅。谢谢。

猜你喜欢

转载自blog.csdn.net/gloryFlow/article/details/131605277
今日推荐