国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 系統 > iOS > 正文

利用iOS開發實現翻轉撲克牌動畫的方法

2019-10-21 18:45:26
字體:
來源:轉載
供稿:網友

前言

本文主要給大家介紹的關于利用iOS開發實現翻轉撲克牌動畫的方法,分享出來供大家參考學習,下面話不多說,來一起看看詳細的介紹吧。

動畫效果

ios,翻轉動畫,view正反翻轉動畫,翻頁動畫效果

實現原理

實現原理就是創建三個撲克牌pockerView , 先在撲克牌上添加一個imageview,作為牌的背面。然后實現ios/81711.html">ios/191565.html">翻轉動畫,在翻轉的時候將imageview移除,添加另一個imageview作為正面。

核心代碼:

方法一: 翻轉動畫,內部實現還是方法二

+ (void)transitionWithView:(UIView *)view duration:(NSTimeInterval)duration options:(UIViewAnimationOptions)options animations:(void (^ __nullable)(void))animations completion:(void (^ __nullable)(BOOL finished))completion NS_AVAILABLE_IOS(4_0);

方法二 :UIView動畫

[UIView beginAnimations:@"aa" context:nil]; [UIView setAnimationDuration:_duration]; [UIView setAnimationCurve:UIViewAnimationCurveLinear]; [view.imgview1 removeFromSuperview]; [view addSubview:view.imgview2]; [UIView setAnimationTransition:UIViewAnimationTransitionFlipFromLeft forView:view cache:NO]; [UIView commitAnimations];

完整代碼:

ViewController.m文件代碼

#import "ViewController.h"#import "PockerView.h"@interface ViewController ()// 記錄翻第幾張牌@property(nonatomic,assign)NSInteger index;// 動畫時間@property(nonatomic,assign)CGFloat duration;@end@implementation ViewController - (void)viewDidLoad { [super viewDidLoad]; self.view.backgroundColor = [UIColor blackColor]; _duration = 0.5; _index = 0; NSArray *arr = @[@"2.jpg",@"3.jpg",@"4.jpg"]; // 循環創建3張撲克牌 for (int i = 0; i < 3; i++) {  PockerView *pocker = [[PockerView alloc]initWithFrame:CGRectMake(100 + 80 * i, 100, 100, 150) imageName:arr[i]];  pocker.tag = 1000 + i;  [self.view addSubview:pocker]; }} // 點擊空白處觸發- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{ // 執行動畫 [self executeAnimation];}// 執行動畫- (void)executeAnimation{ // 根據tag值取到撲克牌 PockerView *pocker = [self.view viewWithTag:1000+ _index]; // 方法一 [self animationWithView:pocker]; // 方法二// [self rotateWithView:pocker];}// 翻牌動畫方法一(內部實現還是方法二)- (void)animationWithView:(PockerView *)view{ // 延時方法 正在翻轉的牌翻轉一半的時候把它移到視圖最上面來 [self performSelector:@selector(delayAction:) withObject:view afterDelay:_duration / 2]; // 翻轉動畫 UIViewAnimationOptions option = UIViewAnimationOptionTransitionFlipFromLeft; [UIView transitionWithView:view      duration:_duration      options:option     animations:^ {      [view.imgview1 removeFromSuperview];      [view addSubview:view.imgview2];     }     completion:^(BOOL finished){      _index++;      if (_index < 3) {       [self executeAnimation];      } }];}// 延時方法- (void)delayAction:(UIView *)view{ [self.view bringSubviewToFront:view];}- (void)delayAction2{ _index++; if (_index < 3) {  [self executeAnimation]; }}// 方法二- (void)rotateWithView:(PockerView *)view{ [self performSelector:@selector(delayAction:) withObject:view afterDelay:_duration / 2]; [self performSelector:@selector(delayAction2) withObject:nil afterDelay:_duration]; [UIView beginAnimations:@"aa" context:nil]; [UIView setAnimationDuration:_duration]; [UIView setAnimationCurve:UIViewAnimationCurveLinear]; [view.imgview1 removeFromSuperview]; [view addSubview:view.imgview2]; [UIView setAnimationTransition:UIViewAnimationTransitionFlipFromLeft forView:view cache:NO]; [UIView commitAnimations];}@end

PockerView.h文件代碼

//// PockerView.h// 翻牌//// Created by 斌 on 2017/4/20.// Copyright © 2017年 斌. All rights reserved.//#import <UIKit/UIKit.h>@interface PockerView : UIView@property(nonatomic,strong)UIImageView *imgview1;@property(nonatomic,strong)UIImageView *imgview2;- (instancetype)initWithFrame:(CGRect)frame imageName:(NSString *)imageName;@end

PockerView.m文件代碼

//// PockerView.m// 翻牌//// Created by 斌 on 2017/4/20.// Copyright © 2017年 斌. All rights reserved.//#import "PockerView.h"@implementation PockerView- (instancetype)initWithFrame:(CGRect)frame imageName:(NSString *)imageName{ self = [super initWithFrame:frame]; if (self) {  // 設置陰影  self.layer.shadowColor = [UIColor blackColor].CGColor;  self.layer.shadowOffset = CGSizeMake(-10, 0);  self.layer.shadowOpacity = 0.3;  // 牌的背面  self.imgview1 = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, frame.size.width, frame.size.height)];  _imgview1.backgroundColor = [UIColor redColor];  _imgview1.image = [UIImage imageNamed:@"1.jpeg"];  [self addSubview:_imgview1];  self.imgview1.layer.cornerRadius = 10;  self.imgview1.clipsToBounds = YES;  self.imgview1.layer.borderWidth = 5;  self.imgview1.layer.borderColor = [[UIColor whiteColor] CGColor];  // 牌的正面  self.imgview2 = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, frame.size.width, frame.size.height)];  _imgview2.backgroundColor = [UIColor redColor];  _imgview2.image = [UIImage imageNamed:imageName];  self.imgview2.layer.cornerRadius = 10;  self.imgview2.clipsToBounds = YES;  self.imgview2.layer.borderWidth = 5;  self.imgview2.layer.borderColor = [[UIColor whiteColor] CGColor]; } return self;}@end

github鏈接地址:https://github.com/jiangbin1993/pockerRotateAnimation.git

 

總結

以上就是這篇文章的全部內容,希望本文的內容對大家的學習或者工作能帶來一定的幫助,如果有疑問大家可以留言交流,謝謝大家對VEVB武林網的支持。

 



注:相關教程知識閱讀請移步到IOS開發頻道。
發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 凌云县| 阳山县| 太白县| 阳朔县| 武清区| 平度市| 光山县| 南溪县| 南平市| 丘北县| 托里县| 灵石县| 广宁县| 育儿| 大英县| 墨竹工卡县| 会同县| 沈阳市| 稻城县| 玉田县| 尚志市| 犍为县| 武陟县| 鄂尔多斯市| 彩票| 贵南县| 汉源县| 淳化县| 买车| 昭觉县| 龙游县| 颍上县| 建湖县| 阿坝县| 开阳县| 赞皇县| 新乡县| 石楼县| 宜宾市| 建湖县| 新兴县|