• 【IOS-初学】利用分段选择器和滑动开关(条)等等实现简单的图片浏览器-透明度和图片切换功能


    背景

    android开发想要学习一点iOS的开发思想和其代码逻辑;
    学习参考:mooc的ios开发
    在这里插入图片描述

    实现的效果

    请添加图片描述

    实现过程

    在这里插入图片描述

    资源准备

    图片等等资源可以自己去网络自定义下载,也可以去mooc的课堂上下载

    UI构建

    在短暂接触iOS开发中,我发现iOS开发十分关注视图和逻辑的分离;有点像咱在android开发中的liveData【采用MVVM】的模式。虽然该模式我现在比较少用,走古典派的,但是在之前的kotlin开发时候对该模式是主流的操作。简单来说,就是不是特陌生的一个编程模式思想。

    在这里插入图片描述
    首先在stroyboard上构建好UI(就是简单拖拽和工具栏中进行简单的设置);然后通过连接的方式,(听iOS的同事说,我这种方式是,弱连接-暂时不太懂这个是个啥?!),那么就在这个m文件可以对UI进行相关的代码逻辑调用。

    代码驱动

    首先post整个项目中最核心的代码:

    //
    //  ViewController.m
    //  picturesBrowser
    //
    //  Created by Chris on 2022/8/31.
    //
    
    #import "ViewController.h"
    
    @interface ViewController ()
    - (IBAction)prePictureButton:(id)sender;
    - (IBAction)nextPictureButton:(id)sender;
    @property (weak, nonatomic) IBOutlet UIImageView *picView;
    @property (weak, nonatomic) IBOutlet UILabel *curFileName;
    @property (weak, nonatomic) IBOutlet UILabel *curAlpha;
    - (IBAction)sliderChanger:(id)sender;
    - (IBAction)segChanger:(id)sender;
    - (IBAction)swichChanger:(id)sender;
    @property (weak, nonatomic) IBOutlet UIImageView *wgView;
    
    @end
    
    @implementation ViewController
    
    // 全局声明一个变量来获取图片信息
    int i;
    - (void)viewDidLoad {
        [super viewDidLoad];
        // Do any additional setup after loading the view.
        // 初始化的时候就对全局变量赋值
        i = 1;
        // 对蜗牛🐌图片的初始化操作
        NSString* fileName;
        // 初始化数组
        NSMutableArray *picWgfiles = [[NSMutableArray alloc]init];
        for(int i = 0; i <= 11; i++){
            fileName= [NSString stringWithFormat:@"10%d.jpg",i];
            [picWgfiles addObject:[UIImage imageNamed:fileName]];
        }
        
        _wgView.animationImages = picWgfiles;
        // 调用播放方法
        [_wgView startAnimating];
    }
    
    // package the method for the repeat function
    - (void) animationWithIndex : (int ) index{
        NSString* fileName = [NSString stringWithFormat:@"p%d.jpg",index];
        _picView.image = [UIImage imageNamed:fileName];
        _curFileName.text =[NSString stringWithFormat:@"当前文件为%@",fileName];
    }
    
    // 向后播放图片
    - (IBAction)nextPictureButton:(id)sender {
        i++;
        if(i == 10){
            i = 1;
        }
        // 获取图片
        [self animationWithIndex:i];
    }
    
    - (IBAction)prePictureButton:(id)sender {
        i--;
        if(i == 0){
            i = 9;
        }
        // 获取图片
        [self animationWithIndex:i];
    }
    
    // 透明度控制
    - (IBAction)sliderChanger:(id)sender {
        UISlider* silder = (UISlider *)sender;
        // 透明度就为实际的滑动的数值
        _picView.alpha = silder.value;
        _curAlpha.text = [NSString stringWithFormat:@"当前透明度为%.1f",silder.value];
    }
    
    // 分段控制的方法
    - (IBAction)segChanger:(id)sender {
        // 对象转化
        UISegmentedControl* seg = (UISegmentedControl *)sender;
        [self alphaWithIndex:(int)seg.selectedSegmentIndex];
       
      
    }
    
    // 透明度方法
    - (void) alphaWithIndex : (int ) index{
        _picView.alpha = 0.2*index;
        _curAlpha.text = [NSString stringWithFormat:@"当前透明度为%.1f",_picView.alpha];
    }
    
    
    - (IBAction)swichChanger:(id)sender {
        UISwitch* switcher = (UISwitch *) sender;
        if(switcher.on == NO){
            // 停止播放
            [_wgView stopAnimating];
        }else{
            // 开始播放
            [_wgView startAnimating];
        }
        
    }
    
    @end
    
    
    • 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

    首先viewDidLoad方法的作用如夏,当view加载好之后就会用这个方法

    • (void)viewDidLoad; // Called after the view has been loaded. For view controllers created in code, this is after -loadView. For view controllers unarchived from a nib, this is after the view is set.`

    所以在这个方法内,加载相关的图片资源。

     // Do any additional setup after loading the view.
        // 初始化的时候就对全局变量赋值
        i = 1;
        // 对蜗牛🐌图片的初始化操作
        NSString* fileName;
        // 初始化数组
        NSMutableArray *picWgfiles = [[NSMutableArray alloc]init];
        for(int i = 0; i <= 11; i++){
            fileName= [NSString stringWithFormat:@"10%d.jpg",i];
            [picWgfiles addObject:[UIImage imageNamed:fileName]];
        }
        
        _wgView.animationImages = picWgfiles;
        // 调用播放方法
        [_wgView startAnimating];
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15

    之后是对每一个方法具体的操作:我对整体的代码进行了方法整合统一和mooc上不一样。
    例如,封装图片切换方法,有点像java中一些调用方式又有点想kotlin的参数加入,反正语言这种东西能用就是牛逼~

    // package the method for the repeat function
    - (void) animationWithIndex : (int ) index{
        NSString* fileName = [NSString stringWithFormat:@"p%d.jpg",index];
        _picView.image = [UIImage imageNamed:fileName];
        _curFileName.text =[NSString stringWithFormat:@"当前文件为%@",fileName];
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6

    其他的方法就是专门针对UI上的一些根据实际业务action做出的反应。

    简单总结

    • 目前我并不是直接专门学习ObjectC的语言来开发iOS,只是先跟着教学内容走;对一些语法只能照瓜画瓢。
    • 两者移动端的存在很多相似点,例如MVVM的编程思想在两端如阴阳两极一样交融,很多东西都是值得融汇贯通的。
  • 相关阅读:
    Android 启动模式梳理
    安科瑞故障电弧探测器在建筑电气的设计与应用
    Java 内存模型 JMM
    魔法导航菜单
    docker安装(Elasticsearch、kibana、IK分词器)8.4.3
    SpringBoot MyBatisPlus Oracle
    NLP新手入门指南|北大-TANGENT
    【红队】要想加入红队,需要做好哪些准备?
    深度学习(三)—— 神经元与神经网络
    Spring Initializr方式构建Spring Boot项目
  • 原文地址:https://blog.csdn.net/weixin_44002043/article/details/126892564