iOS老虎機(jī)的實(shí)現(xiàn)

??: 項(xiàng)目需要使用pods進(jìn)行管理開(kāi)發(fā),這里需要依賴的第三方庫(kù)有SDWebImage、pop

1问顷、直接上代碼

SlotsItemView.h

//
//  SlotsItemView.h
//
//  Created by wsj_2012 on 2017/8/18.
//  Copyright ? 2017年 wsj_2012. All rights reserved.
//

#import <UIKit/UIKit.h>

@class SlotsItemView;
@protocol SlotsItemViewDelegate <NSObject>

- (void)slotsItemView:(SlotsItemView *)itemView animationDidFinished:(BOOL)finished;

@end

@interface SlotsItemView : UIView

@property (nonatomic, strong) NSArray<NSString *> *data;
@property (nonatomic, strong) NSString *result;

@property (nonatomic) NSInteger index;
@property (nonatomic) CFTimeInterval unitInterval;
@property (nonatomic) CGFloat decayRatio; // 0 ~ 0.2;

@property (nonatomic, weak) id <SlotsItemViewDelegate> delegate;

- (void)start;

- (void)decelerate;

@end

SlotsItemView.m

//
//  SlotsItemView.m
//
//  Created by wsj_2012 on 2017/8/18.
//  Copyright ? 2017年 wsj_2012. All rights reserved.
//

#import "SlotsItemView.h"
#import <pop/POP.h>

@interface SlotsContentView : UIView

@property (nonatomic, strong) UIImageView *imageView;
@property (nonatomic, strong) UILabel *label;
@property (nonatomic, strong) NSString *imageName;

@end

@implementation SlotsContentView

- (instancetype)initWithFrame:(CGRect)frame
{
    if (self = [super initWithFrame:frame]) {
        _imageView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, frame.size.width, frame.size.height)];
        _imageView.contentMode = UIViewContentModeScaleToFill;
        _imageView.image = [UIImage imageNamed:@"sign_slots_bg" inBundle:[NSBundle bundleForClass:self.class] compatibleWithTraitCollection:nil];
        _imageView.contentMode = UIViewContentModeScaleAspectFit;
        [self addSubview:_imageView];
        
        _label = [[UILabel alloc] initWithFrame:_imageView.frame];
        _label.textAlignment = NSTextAlignmentCenter;
        _label.font = [UIFont systemFontOfSize:30];
        [self addSubview:_label];
    }
    return self;
}

- (void)setImageName:(NSString *)imageName
{
    ///這里使用到了自己加工的基礎(chǔ)擴(kuò)展庫(kù),也可以直接使用SDWebImage庫(kù)
    [self.imageView ydt_setImageWithURLString:imageName placeholder:nil];
}

@end

static const CGFloat kFinalInterval = 1.f;
static const CGFloat kDecayStep = 0.5f;

@interface SlotsItemView () <POPAnimationDelegate>

@property (nonatomic, strong) SlotsContentView *contentView1;
@property (nonatomic, strong) SlotsContentView *contentView2;
@property (nonatomic, strong) SlotsContentView *showView;
@property (nonatomic, strong) SlotsContentView *readyView;

@property (nonatomic) CGFloat width;
@property (nonatomic) CGFloat height;
@property (nonatomic) BOOL isDecelerate;
@property (nonatomic) CGFloat step;

@end

@implementation SlotsItemView

- (instancetype)initWithFrame:(CGRect)frame
{
    if (self = [super initWithFrame:frame]) {
        self.clipsToBounds = YES;
        _index = 0;
        _unitInterval = 0.1;
        _step = 0;
        CGRect frame1 = CGRectMake(0, 0, frame.size.width, frame.size.height);
        CGRect frame2 = CGRectMake(0, frame.size.height, frame.size.width, frame.size.height);
        
        _contentView1 = [[SlotsContentView alloc] initWithFrame:frame1];
        [self addSubview:_contentView1];
        _showView = _contentView1;
        
        _contentView2 = [[SlotsContentView alloc] initWithFrame:frame2];
        [self addSubview:_contentView2];
        _readyView = _contentView2;
    }
    return self;
}

- (void)start
{
    if (self.data.count == 0) {
        return;
    }
    [self reset];
    [self animate1];
}

- (void)decelerate
{
    self.isDecelerate = YES;
}

- (CGFloat)deltaInterval
{
    self.step += kDecayStep;
    return self.decayRatio * self.step * self.step;
}

- (void)reset
{
    _index = 0;
    _unitInterval = 0.1;
    _step = 0;
    _isDecelerate = NO;
    self.bounds = CGRectMake(0, 0, self.width, self.height);
    self.contentView1.frame = CGRectMake(0, 0, self.width, self.height);
    self.contentView2.frame = CGRectMake(0, self.height, self.width, self.height);
}

- (void)animate1
{
    if (self.isDecelerate) {
        self.unitInterval += [self deltaInterval];
    }
    
    CGRect bounds = CGRectMake(0, (_index + 1) * self.height, self.width, self.height);
    CGRect frame1 = CGRectMake(0, (_index + 2) * self.height, self.width, self.height);
    __weak typeof(self) weakSelf = self;
    POPBasicAnimation *animation = [POPBasicAnimation animationWithPropertyNamed:kPOPViewBounds];
    animation.delegate = self;
    animation.toValue = [NSValue valueWithCGRect:bounds];
    animation.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionLinear];
    animation.duration = _unitInterval;
    animation.completionBlock = ^(POPAnimation *anim, BOOL finished) {
        if (finished) {
            weakSelf.index += 1;
            weakSelf.contentView1.frame = frame1;
            weakSelf.contentView1.imageName = weakSelf.data[weakSelf.index % weakSelf.data.count];
            [weakSelf animate2];
        }
    };
    
    if (self.unitInterval > kFinalInterval) {
        self.contentView2.imageName = self.result;
        animation.completionBlock = nil;
        self.showView = self.contentView2;
        self.readyView = self.contentView1;
    }
    
    [self pop_addAnimation:animation forKey:@"animate1"];
}


- (void)animate2
{
    if (self.isDecelerate) {
        self.unitInterval += [self deltaInterval];
    }
    
    CGRect bounds = CGRectMake(0, (_index + 1) * self.height, self.width, self.height);
    CGRect frame2 = CGRectMake(0, (_index + 2) * self.height, self.width, self.height);
    __weak typeof(self) weakSelf = self;
    POPBasicAnimation *animation = [POPBasicAnimation animationWithPropertyNamed:kPOPViewBounds];
    animation.delegate = self;
    animation.toValue = [NSValue valueWithCGRect:bounds];
    animation.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionLinear];
    animation.duration = _unitInterval;
    animation.completionBlock = ^(POPAnimation *anim, BOOL finished) {
        if (finished) {
            weakSelf.index += 1;
            weakSelf.contentView2.frame = frame2;
            weakSelf.contentView2.imageName = weakSelf.data[weakSelf.index % weakSelf.data.count];
            [weakSelf animate1];
        }
    };
    
    
    if (self.unitInterval > kFinalInterval) {
        self.contentView1.imageName = self.result;
        animation.completionBlock = nil;
        self.showView = self.contentView1;
        self.readyView = self.contentView2;
    }
    
    [self pop_addAnimation:animation forKey:@"animate2"];
}

- (void)setData:(NSArray<NSString *> *)data
{
    _data = data;
    self.showView.imageName = _result ?: data.firstObject;
    self.readyView.imageName = data[(_index + 1) % data.count];
}

- (CGFloat)width
{
    return self.frame.size.width;
}

- (CGFloat)height
{
    return self.frame.size.height;
}

- (void)pop_animationDidStop:(POPAnimation *)anim finished:(BOOL)finished
{
    if (anim.completionBlock == nil) {
        if ([self.delegate respondsToSelector:@selector(slotsItemView:animationDidFinished:)]) {
            [self.delegate slotsItemView:self animationDidFinished:YES];
        }
    }
}

@end

SlotsView.h

//
//  SlotsView.h
//
//  Created by wsj_2012 on 2017/8/18.
//  Copyright ? 2017年 wsj_2012. All rights reserved.
//

#import <UIKit/UIKit.h>

@protocol SlotsViewDelegate <NSObject>

///動(dòng)畫(huà)結(jié)束代理
- (void)slotsViewDidFinishedAnimation;

@end

@interface SlotsView : UIView

@property (nonatomic, strong) NSArray<NSString *> *data1;
@property (nonatomic, strong) NSArray<NSString *> *data2;
@property (nonatomic, strong) NSArray<NSString *> *data3;
@property (nonatomic, strong) NSArray<NSString *> *results; // 必須3個(gè)

@property (nonatomic, weak) id <SlotsViewDelegate> delegate;

- (void)start;
- (void)end;

@end

SlotsView.m

//
//  SlotsView.m
//
//  Created by wsj_2012 on 2017/8/18.
//  Copyright ? 2017年 wsj_2012. All rights reserved.
//

#import "SlotsView.h"
#import "SlotsItemView.h"
#import <Masonry/Masonry.h>

@interface SlotsView () <SlotsItemViewDelegate>

@property (nonatomic, strong) UIImageView *imageView;
@property (nonatomic, strong) SlotsItemView *item1;
@property (nonatomic, strong) SlotsItemView *item2;
@property (nonatomic, strong) SlotsItemView *item3;
@property (nonatomic) NSInteger flag;

@end

@implementation SlotsView

- (instancetype)initWithFrame:(CGRect)frame
{
    if (self = [super initWithFrame:frame]) {
        _flag = 3;
        CGFloat width = (frame.size.width - 48) / 3;
        CGFloat height = frame.size.height;
        
        _item1 = [[SlotsItemView alloc] initWithFrame:CGRectMake(14, 0, width, height)];
        _item1.decayRatio = 0.15;
        _item1.delegate = self;
        [self addSubview:_item1];
        
        _item2 = [[SlotsItemView alloc] initWithFrame:CGRectMake(24 + width, 0, width, height)];
        _item2.decayRatio = 0.1;
        _item2.delegate = self;
        [self addSubview:_item2];
        
        _item3 = [[SlotsItemView alloc] initWithFrame:CGRectMake(34 + width * 2, 0, width, height)];
        _item3.decayRatio = 0.05;
        _item3.delegate = self;
        [self addSubview:_item3];
        
    }
    return self;
}

- (void)start
{
    self.flag = 3;
    [self.item1 start];
    dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.2 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
        [self.item2 start];
    });
    dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.5 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
        [self.item3 start];
    });
}

- (void)end
{
    [self.item1 decelerate];
    dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.1 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
        [self.item2 decelerate];
    });
    dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.4 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
        [self.item3 decelerate];
    });
}

- (void)setData1:(NSArray<NSString *> *)data1
{
    _data1 = data1;
    self.item1.data = data1;
}

- (void)setData2:(NSArray<NSString *> *)data2
{
    _data2 = data2;
    self.item2.data = data2;
}

- (void)setData3:(NSArray<NSString *> *)data3
{
    _data3 = data3;
    self.item3.data = data3;
}

- (void)setResults:(NSArray<NSString *> *)results
{
    _results = results;
    self.item1.result = results.firstObject;
    if (results.count > 1) {
        self.item2.result = results[1];
    }
    if (results.count > 2) {
        self.item3.result = results[2];
    }
}

- (void)slotsItemView:(SlotsItemView *)itemView animationDidFinished:(BOOL)finished
{
    self.flag -= 1;
    if (self.flag == 0 && [self.delegate respondsToSelector:@selector(slotsViewDidFinishedAnimation)]) {
        [self.delegate slotsViewDidFinishedAnimation];
    }
}

@end

2、使用

  • 在要使用的類創(chuàng)建對(duì)象并關(guān)聯(lián)代理YZTSlotsViewDelegate
    /// 聲明為全局變量
    @property (nonatomic, strong) YZTSlotsView *slotsView;

    _slotsView = [[SlotsView alloc] initWithFrame:CGRectMake(0, 42, YZTScreenWidth - 28, 150)];
    _slotsView.delegate = self;
    _slotsView.layer.cornerRadius = 5.0f;
    [lottery addSubview:_slotsView];
  • 數(shù)據(jù)塞給對(duì)象
    NSMutableArray *images = [NSMutableArray array];
    for (PrizeItemModel *model in lotteryPrizeList) {
        !model.imageUrl ? : [images addObject:model.imageUrl];
        /// 使用了SDWebImage緩存圖片邓深,以后每次進(jìn)來(lái)不用每次都下載圖片
        [[UIImageView new] yzt_setImageWithURLString:model.imageUrl];
    }
    self.slotsView.data1 = images;
    self.slotsView.data2 = images;
    self.slotsView.data3 = images;
  • 獲取到老虎機(jī)結(jié)果調(diào)用end方法,并把結(jié)果傳給results
    self.slotsView.results = @[self.prizeModel.imageUrl, self.prizeModel.imageUrl, self.prizeModel.imageUrl]; 
    [self.slotsView end];
  • 處理老虎節(jié)停止?jié)L動(dòng)后的代理
- (void)slotsViewDidFinishedAnimation
{
    if (self.comlotteryModel.winflag == 1) {
        self.topLabel.text = [NSString stringWithFormat:@"恭喜你抽中%@", self.prizeModel.name];
    }else {
        self.topLabel.text = @"謝謝參與笔刹!";
    }
    self.lotteryButton.enabled = YES;
    [self.tableView reloadData];
}
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
  • 序言:七十年代末芥备,一起剝皮案震驚了整個(gè)濱河市,隨后出現(xiàn)的幾起案子舌菜,更是在濱河造成了極大的恐慌萌壳,老刑警劉巖,帶你破解...
    沈念sama閱讀 221,635評(píng)論 6 515
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件日月,死亡現(xiàn)場(chǎng)離奇詭異袱瓮,居然都是意外死亡,警方通過(guò)查閱死者的電腦和手機(jī)爱咬,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 94,543評(píng)論 3 399
  • 文/潘曉璐 我一進(jìn)店門尺借,熙熙樓的掌柜王于貴愁眉苦臉地迎上來(lái),“玉大人精拟,你說(shuō)我怎么就攤上這事燎斩∈幔” “怎么了?”我有些...
    開(kāi)封第一講書(shū)人閱讀 168,083評(píng)論 0 360
  • 文/不壞的土叔 我叫張陵栅表,是天一觀的道長(zhǎng)笋鄙。 經(jīng)常有香客問(wèn)我,道長(zhǎng)怪瓶,這世上最難降的妖魔是什么萧落? 我笑而不...
    開(kāi)封第一講書(shū)人閱讀 59,640評(píng)論 1 296
  • 正文 為了忘掉前任,我火速辦了婚禮劳殖,結(jié)果婚禮上铐尚,老公的妹妹穿的比我還像新娘。我一直安慰自己哆姻,他們只是感情好宣增,可當(dāng)我...
    茶點(diǎn)故事閱讀 68,640評(píng)論 6 397
  • 文/花漫 我一把揭開(kāi)白布。 她就那樣靜靜地躺著矛缨,像睡著了一般爹脾。 火紅的嫁衣襯著肌膚如雪。 梳的紋絲不亂的頭發(fā)上箕昭,一...
    開(kāi)封第一講書(shū)人閱讀 52,262評(píng)論 1 308
  • 那天灵妨,我揣著相機(jī)與錄音,去河邊找鬼落竹。 笑死泌霍,一個(gè)胖子當(dāng)著我的面吹牛,可吹牛的內(nèi)容都是我干的述召。 我是一名探鬼主播朱转,決...
    沈念sama閱讀 40,833評(píng)論 3 421
  • 文/蒼蘭香墨 我猛地睜開(kāi)眼,長(zhǎng)吁一口氣:“原來(lái)是場(chǎng)噩夢(mèng)啊……” “哼积暖!你這毒婦竟也來(lái)了藤为?” 一聲冷哼從身側(cè)響起,我...
    開(kāi)封第一講書(shū)人閱讀 39,736評(píng)論 0 276
  • 序言:老撾萬(wàn)榮一對(duì)情侶失蹤夺刑,失蹤者是張志新(化名)和其女友劉穎缅疟,沒(méi)想到半個(gè)月后,有當(dāng)?shù)厝嗽跇?shù)林里發(fā)現(xiàn)了一具尸體遍愿,經(jīng)...
    沈念sama閱讀 46,280評(píng)論 1 319
  • 正文 獨(dú)居荒郊野嶺守林人離奇死亡存淫,尸身上長(zhǎng)有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點(diǎn)故事閱讀 38,369評(píng)論 3 340
  • 正文 我和宋清朗相戀三年,在試婚紗的時(shí)候發(fā)現(xiàn)自己被綠了错览。 大學(xué)時(shí)的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片纫雁。...
    茶點(diǎn)故事閱讀 40,503評(píng)論 1 352
  • 序言:一個(gè)原本活蹦亂跳的男人離奇死亡,死狀恐怖倾哺,靈堂內(nèi)的尸體忽然破棺而出轧邪,到底是詐尸還是另有隱情刽脖,我是刑警寧澤,帶...
    沈念sama閱讀 36,185評(píng)論 5 350
  • 正文 年R本政府宣布忌愚,位于F島的核電站曲管,受9級(jí)特大地震影響,放射性物質(zhì)發(fā)生泄漏硕糊。R本人自食惡果不足惜院水,卻給世界環(huán)境...
    茶點(diǎn)故事閱讀 41,870評(píng)論 3 333
  • 文/蒙蒙 一、第九天 我趴在偏房一處隱蔽的房頂上張望简十。 院中可真熱鬧檬某,春花似錦、人聲如沸螟蝙。這莊子的主人今日做“春日...
    開(kāi)封第一講書(shū)人閱讀 32,340評(píng)論 0 24
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽(yáng)胰默。三九已至场斑,卻和暖如春,著一層夾襖步出監(jiān)牢的瞬間牵署,已是汗流浹背漏隐。 一陣腳步聲響...
    開(kāi)封第一講書(shū)人閱讀 33,460評(píng)論 1 272
  • 我被黑心中介騙來(lái)泰國(guó)打工, 沒(méi)想到剛下飛機(jī)就差點(diǎn)兒被人妖公主榨干…… 1. 我叫王不留奴迅,地道東北人青责。 一個(gè)月前我還...
    沈念sama閱讀 48,909評(píng)論 3 376
  • 正文 我出身青樓,卻偏偏與公主長(zhǎng)得像取具,于是被迫代替她去往敵國(guó)和親爽柒。 傳聞我的和親對(duì)象是個(gè)殘疾皇子,可洞房花燭夜當(dāng)晚...
    茶點(diǎn)故事閱讀 45,512評(píng)論 2 359

推薦閱讀更多精彩內(nèi)容