IOS 控件常识


//获取图片或文件方式
1、通过plist文件获取图片名
NSString *plisFile = [[NSBundle mainBundle] pathForResource:@“文件名” ofType:@"plist"];
    NSArray *array = [[NSArray alloc] initWithContentsOfFile:plistFile];
//多文件操作   找到包中所有plis文件 然后过滤info文件 遍历数组 找到图片并保存到数组中去
 NSArray *plists = [[NSBundle mainBundle] pathsForResourcesOfType:@"plist" inDirectory:nil];//找到包中所有的plist文件
    for (NSString *file in plists) {
        //找到最后一个'/'的位置
        NSRange range = [file rangeOfString:@"/" options:NSBackwardsSearch];
        //提取文件名
        NSString *name = [file substringFromIndex:range.location+1];
        //过滤Info.plist
        if ([name isEqualToString:@"Info.plist"]) {
            continue;
        }
        //读取文件内容
        NSArray *array = [NSArray arrayWithContentsOfFile:file];
        //创建用于保存所有图片名字的数组
        NSMutableArray *imageNames = [NSMutableArray array];
        //遍历数组提取图片名字并保存
        for (NSDictionary *dic in array) {
            [imageNames addObject:dic[@"imageName"]];
        }
//通过找到包路径 遍历包路径内容找出后缀是png格式的图片  并存到数组中去
NSString *bundlePath = [NSBundle mainBundle].bundlePath;//找到包路径
   NSArray *array = [[NSFileManager defaultManager] contentsOfDirectoryAtPath:bundlePath error:nil];  //遍历目录内容
    //遍历数组提取有效文件
    for (NSString *fileName in array) {
        if (![fileName hasSuffix:@"png"]) {
            continue;
        }
    view.image = [UIImage imageNamed:fileName];
}
//根据文件名字查找包路径
NSString *path = [[NSBundle mainBundle] pathForResource:@"qq" ofType:@"jpg"];
UIImage *image = [[UIImage alloc] initWithContentsOfFile:path];


//图片相关
view.contentMode = UIViewContentModeScaleAspectFit;//图片的填充模式
imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal]; //设置图片选染色
//图片拉伸
stretchableImageWithLeftCapWidth
resizableImageWithCapInsets:
//贴图
 //找到图片的名字
    NSString *name = 图片名字;
    NSArray *names = [name componentsSeparatedByString:@"."];
    //找到在包中的路径
    NSString *path = [[NSBundle mainBundle] pathForResource:names[0] ofType:names[1]];
    //贴图
    view.image = [UIImage imageWithContentsOfFile:path];
//添加环境变量
 Uuild Settings搜索 prefix header
Precompile Prefix Header 修改Yes
Prefix Header 放路径    $(SRCROOT)/D2TableViewBaseOperate/utiliity.pch  //注意默认路径是根目录
UICollectionView  集合视图  <UICollectionViewDelegateFlowLayout, UICollectionViewDataSource>  
//创建时需要指定尺寸和布局对象
_collectionView = [[UICollectionView alloc] initWithFrame:frame collectionViewLayout:[self createLayout]];
//注册附加视图类型及复用标识
    [_collectionView registerClass:[UICollectionReusableView class] forSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"headerId"];
- (UICollectionViewFlowLayout *)createLayout
layout.minimumLineSpacing = 10;//最小行间距
layout.minimumInteritemSpacing = 10;//item的最小间距
layout.itemSize = CGSizeMake(100, 100);//item尺寸
layout.sectionInset = UIEdgeInsetsMake(20, 20, 20, 20);//组的四周边距
//垂直滚动,高度有效;水平滚动,宽度有效
layout.headerReferenceSize = CGSizeMake(50, 50);
layout.footerReferenceSize = CGSizeMake(50, 50);
//附加视图是否一直显示在边界
layout.sectionHeadersPinToVisibleBounds = YES;
layout.sectionFootersPinToVisibleBounds = YES;
//代理方法
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
    return 60;
}
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
    UICollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"cellId" forIndexPath:indexPath];
    return cell;
}
- (UICollectionReusableView *)collectionView:(UICollectionView *)collectionView viewForSupplementaryElementOfKind:(NSString *)kind atIndexPath:(NSIndexPath *)indexPath {
if ([kind isEqualToString:UICollectionElementKindSectionHeader]) {
//头视图
UICollectionReusableView *view = [collectionView dequeueReusableSupplementaryViewOfKind:kind withReuseIdentifier:@"headerId" forIndexPath:indexPath];
        view.backgroundColor = [UIColor greenColor];
        return view;
    }
}
//通过代理方法可以动态指定布局对象的相关属性
//指定后布局依次为准,创建布局对象时的默认设置无效
- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath {
return CGSizeMake(150, 100);
}

//UITableView 表格视图 <UITableViewDataSource> <UITableViewDelegate> day10
self.automaticallyAdjustsScrollViewInsets = NO; //取消导航视图影响
//注册cell类型及复用标识  定制的Cell 不用写
[_tableView registerClass:[UITableViewCell class] forCellReuseIdentifier:@"cellId"];

//返回组数
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return _dataSource.count;
}
//返回指定的组的行数
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return [_dataSource[section] count];
}
//返回选中的cell的IndexPath
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    //表格视图是通过一个复用队列来管理cell的,每次需要时先从队列中查找,若有空闲的直接返回,没有时返回nil,返回nil时需要创建cell,若之前已经注册则会直接创建,然后返回;当cell被滑出显示范围就会被放到复用队列中
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cellId"];
 //     indexPath.section //组号     indexPath.row 行号
}

//添加右上角 item
UIBarButtonItem *item = [[UIBarButtonItem alloc] initWithTitle:@"Edit" style:UIBarButtonItemStylePlain target:self action:@selector(itemHandle:)];
self.navigationItem.rightBarButtonItem = item;
//返回表格视图的编辑状态
UITableViewCellEditingStyleNone,  //无效果
UITableViewCellEditingStyleDelete,  //删除
UITableViewCellEditingStyleInsert   //添加
//注意这里可以用  位或 | 在实现多个风格的调用
- (UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath {
    return UITableViewCellEditingStyleInsert;
}
//表格视图可编辑状态时 会回调这个代理方法 可在这里做编辑操作
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath {
        //操作完之后要刷新显示   // 这里默认是数组  所以我们要写成数组形式
        //添加
        [self.tableView insertRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationAutomatic];
        //删除
        [tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationAutomatic];
//刷新指定的组的数据
        [self.tableView reloadSections:[NSIndexSet indexSetWithIndex:section] withRowAnimation:UITableViewRowAnimationAutomatic];
}
//返回删除按钮的标题  这个方法可以修改删除按钮的标题
- (NSString *)tableView:(UITableView *)tableView titleForDeleteConfirmationButtonForRowAtIndexPath:(NSIndexPath *)indexPath {
    return @"删除";
}
//当全选按钮的时候 获取指定区域的cell的indexPath 我们可以用
[self.tableView indexPathsForRowsInRect:
//使用代码方式选中一行
[self.tableView selectRowAtIndexPath:indexPath animated:YES scrollPosition:UITableViewScrollPositionNone];
//使用代码方式取消选中一行
[self.tableView deselectRowAtIndexPath:indexPath animated:YES];
 //重新加载所有数据
[self.tableView reloadData];
//移动一个cell 到新的IndexPath时 回调的代理方法
- (void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)sourceIndexPath toIndexPath:(NSIndexPath *)destinationIndexPath{}
//返回组的头视图的高度
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
return 44;
}
//刷新指定的组的数据时 会回调这个方法
- (nullable UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
//刷新指定的组的数据
[self.tableView reloadSections:[NSIndexSet indexSetWithIndex:section] withRowAnimation:UITableViewRowAnimationAutomatic];
}
UITableViewCellStyle  Cell风格
    UITableViewCellStyleDefault,    // 左侧显示textLabel(不显示detailTextLabel),imageView可选(显示在最左边)
    UITableViewCellStyleValue1,        // 左侧显示textLabel、右侧显示detailTextLabel(默认蓝色),imageView可选(显示在最左边)
    UITableViewCellStyleValue2,        // 左侧依次显示textLabel(默认蓝色)和detailTextLabel,imageView可选(显示在最左边)
    UITableViewCellStyleSubtitle    // 左上方显示textLabel,左下方显示detailTextLabel(默认灰色),imageView可选(显示在最左边)
if (!cell) {右侧附加按钮风格
cell.accessoryType = UITableViewCellAccessoryDetailButton;
//右侧附加视图,accessoryType无效
cell.editingAccessoryType = UITableViewCellAccessoryCheckmark;
cell.accessoryView = view;
//编辑状态附加视图,editingAccessoryType无效
 cell.editingAccessoryView = editingView;
//右侧button风格右侧附加视图点击时回调该方法
- (void)tableView:(UITableView *)tableView accessoryButtonTappedForRowWithIndexPath:(NSIndexPath *)indexPath {
printf("section:%ld row:%ld\n",indexPath.section,indexPath.row);
}
3D方式旋转
cell.layer.transform = CATransform3DMakeRotation(flag*M_PI_2, 0, 0, 1.0);
//恢复原始状态
cell.layer.transform = CATransform3DIdentity;
//返回cell的高度
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    return 70;
}
UISearchController 搜索视图
//当搜索框弹出时是否覆盖当前的视图控制器
self.definesPresentationContext = YES;
//搜索时是否隐藏导航条
_searchController.hidesNavigationBarDuringPresentation = NO;
//能否选中搜索到的结果
_searchController.dimsBackgroundDuringPresentation = NO;
UISearchBar 搜索条  UISearchDisplayController  搜索显示控制器
//创建搜索显示控制器,并绑定搜索条,显示到哪个控制器上
_searchDC = [[UISearchDisplayController alloc] initWithSearchBar:_searchBar contentsController:self];


//滚动视图 UIScrollView  <UIScrollViewDelegate> //代理  day09

scrollView.bounces = NO; //禁止出界回弹
scrollView.pagingEnabled = YES; //设置按页滚动
scrollView.indicatorStyle //设置滚动条的风格
scrollView.contentSize //设置内容大小
scrollView.contentOffset //设置偏移量
scrollView.showsHorizontalScrollIndicator = NO;  //不显示滚动条X
scrollView.showsVerticalScrollIndicator = NO;    //不显示滚动条Y
[scrollView setContentOffset:CGPointMake() animated:YES];//设置滚动视图的偏移量
scrollView.decelerationRate = //  滑动离手后自由减速的加速度
UIScrollViewDecelerationRateNormal 正常减速
UIScrollViewDecelerationRateFast   快速减速
scrollView.scrollsToTop = NO    //点击状态栏回滚到顶部,默认为YES
//一直可以滚动,无论内容尺寸大小,需要允许边界回弹  当图片小于内容视图尺寸时 想要右滚动效果需要这是这个
scrollView.alwaysBounceHorizontal = YES;
scrollView.alwaysBounceVertical = YES;

[self performSelector:@selector(选择器方法) withObject:nil afterDelay:3] //延迟3秒
将偏移量设置为(0,0)
[_scrollView setContentOffset:CGPointZero animated:YES]
//缩放设置
scrollView.minimumZoomScale = 0.5;
scrollView.maximumZoomScale = 2.0;
scrollView.zoomScale //缩放值

//代理方法
UIScrollView * sv = [[UIScrollView alloc]init];
[sv setZoomScale:1.0 animated:YES];
- (void)scrollViewDidScroll:(UIScrollView*)scrollView {
    //有任何滚动都会一直调用,不能做耗时操作
    //printf("正在滚动...\n");
}
//拖拽相关
- (void)scrollViewWillBeginDragging:(UIScrollView *)scrollView {
    printf("即将开始拖拽\n");
}
- (void)scrollViewWillEndDragging:(UIScrollView *)scrollView withVelocity:(CGPoint)velocity targetContentOffset:(inout CGPoint *)targetContentOffset {
    printf("即将结束拖拽\n");
}
- (void)scrollViewDidEndDragging:(UIScrollView *)scrollView willDecelerate:(BOOL)decelerate {
    printf("已经结束拖拽\n");
}
//减速相关     
- (void)scrollViewWillBeginDecelerating:(UIScrollView *)scrollView {
    printf("即将开始减速\n");
}
- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView {
    printf("已经结束减速\n");
}
- (void)scrollViewDidEndScrollingAnimation:(UIScrollView *)scrollView {
    printf("使用代码方式滚动结束\n");
}
//点击状态栏自动回滚相关
- (BOOL)scrollViewShouldScrollToTop:(UIScrollView *)scrollView {
    //是否能够回滚到顶部,返回NO不允许回滚到顶部
    return YES;
}
- (void)scrollViewDidScrollToTop:(UIScrollView *)scrollView {
    printf("已经自动回滚到顶部\n");
}
//缩放相关
- (UIView *)viewForZoomingInScrollView:(UIScrollView *)scrollView {
    //返回需要缩放的视图
    return scrollView.subviews[0];
}
- (void)scrollViewDidZoom:(UIScrollView *)scrollView {
    //只要有缩放就一直回调,不能做耗时操作
    //printf("缩放...\n");
}
- (void)scrollViewWillBeginZooming:(UIScrollView *)scrollView withView:(UIView *)view {
    printf("即将开始缩放\n");
}
- (void)scrollViewDidEndZooming:(UIScrollView *)scrollView withView:(UIView *)view atScale:(CGFloat)scale {
    printf("已经缩放结束\n");
}
//按页控制  UIPageControl
pageControl.numberOfPages //总页数
pageControl.currentPage //当前页
pageControl.pageIndicatorTintColor //非选中球的颜色
pageControl.currentPageIndicatorTintColor  //选中球的颜色
[pageControl addTarget:self action:@selector(onClick:) forControlEvents:UIControlEventTouchUpInside]//添加事件;
//UITabBarController  : UITabBarController  <UITabBarControllerDelegate> day07
hidesBottomBarWhenPushed = YES //隐藏tabBar
tbc.tabBar.barTintColor = [UIColor cyanColor];//设置tabBar的背景色
tbc.tabBar.tintColor = [UIColor redColor]; //    改变选中状态的颜色
tbc.tabBar.translucent = YES; //设置为不透明
NSInteger selectedIndex = [[NSUserDefaults standardUserDefaults] integerForKey:@"selectedIndex"]; //从文件中读取出来记录的选中序号
tbc.selectedIndex = selectedIndex; //设置选中的视图控制器
//系统自带item风格
UITabBarSystemItem itemStytles[] = {
        UITabBarSystemItemFavorites,
        UITabBarSystemItemHistory,
        UITabBarSystemItemBookmarks,
        UITabBarSystemItemSearch,
        UITabBarSystemItemDownloads,
        UITabBarSystemItemMostRecent,
    };//根据系统风格创建item
UITabBarItem *item = [[UITabBarItem alloc] initWithTabBarSystemItem:itemStytles[i] tag:i+1];
//自定义item
UIImage *image = [[UIImage imageNamed:imageName]  imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal]; //正常状态图片
UIImage *selectedImage = [[UIImage imageNamed:selectedImageName]     imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal]; //选中状态图片
UITabBarItem *item = [[UITabBarItem alloc] initWithTitle:titles[i][0] image:image selectedImage:selectedImage];  //创建item
//统一设置tabBarItem的标题属性
[[UITabBarItem appearance] setTitleTextAttributes:@{NSForegroundColorAttributeName:color} forState:UIControlStateSelected];
//自定义tabBar
//隐藏系统自带的tabBar
self.tabBar.hidden = YES;
bc.viewControllers = controllers;
//自定义tabBar时 系统会调用
- (void)setViewControllers:(NSArray<__kindof UIViewController *> *)viewControllers
{    [super setViewControllers:viewControllers];
     //布局自己的tabBarView
    [self layoutTabBarView];
}
- (void)changeViewController:(UIButton *)button
{  if (button.tag == self.lastIndex) {
        return;
    }
    //找到原来选中的视图控制器
    BaseViewController *bvc = self.viewControllers[self.lastIndex];
    //取消其选中状态
    bvc.tabBarButton.selected = NO;
    //设置它会切换视图控制器
    self.selectedIndex = button.tag;
    button.selected = YES;
    //记录本次选中的视图控制器
    self.lastIndex = button.tag;
}

//保存选中视图控制器的序号
[[NSUserDefaults standardUserDefaults] setInteger:tabBarController.selectedIndex forKey:@"selectedIndex"];
//上面的方式不一定会立即写入磁盘文件,可以使用下面的方式立即同步写入到磁盘文件中
[[NSUserDefaults standardUserDefaults]synchronize];
//是否可以被选中
- (BOOL)tabBarController:(UITabBarController *)tabBarController shouldSelectViewController:(UIViewController *)viewController
NSHomeDirectory()//沙盒目录
self.tabBar.hidden = YES; //隐藏系统自带的tabBar

//导航控制器   : UIViewController   day05
//创建导航控制器,并制定根控制器
UINavigationController *nc = [[UINavigationController alloc]
initWithRootViewController:rvc];
UINavigationBar *bar = self.navigationController.navigationBar;
bar.barStyle = UIBarStyleBlack;   UIBarStyleDefault  状态栏为黑色
                                      UIBarStyleBlack    状态栏为白色
bar.translucent = NO;半透明效果,为NO时不透明,该属性会影响视图的布局
                      不透明(NO):子视图的参考原点(0,64)
                    半透明(YES):子视图的参考原点(0,0)
bar.barTintColor = [UIColor cyanColor]; //导航条的颜色
//7.0之前可以修改导航条背景色,7.0之后设置返回按钮的颜色
bar.tintColor = [UIColor yellowColor];
UIBarMetricsDefault    竖屏
UIBarMetricsCompact    横屏
[bar setBackgroundImage:[UIImage imageNamed:@"horizontal-bar64"] forBarMetrics:UIBarMetricsDefault];
//设置导航条标题的属性(字典)
bar.titleTextAttributes = @{NSFontAttributeName:[UIFont systemFontOfSize:30],NSForegroundColorAttributeName:[UIColor redColor]};
self.navigationItem.title = @"第二个";
//设置标题视图,设置后标题无效  
//系统风格   dome3   //导航栏上面的按钮
UIBarButtonItem *item1 = [[UIBarButtonItem alloc]
initWithBarButtonSystemItem:UIBarButtonSystemItemAdd target:self action:@selector(barButtonHandle:)];
//自定义方式创建
UIBarButtonItem *item4 = [[UIBarButtonItem alloc] initWithCustomView:button];
nc.childViewControllers[1]  //通过下标可以找到指定的试图控制器
//添加到 BarButtonItems里即可显示   可选择左右两边的位置
self.navigationItem.rightBarButtonItems = @[item1,item2,item3,item4];
//UIToolbar
UIToolbar *bar = self.navigationController.toolbar;
bar.translucent = NO;//透明度
bar.barStyle = UIBarStyleBlack; //设置风格,设置背景色之后无效
bar.barTintColor = [UIColor cyanColor];  //设置背景色
[bar setBackgroundImage:[UIImage imageNamed:@"toolbar44-1"] forToolbarPosition:UIBarPositionAny barMetrics:UIBarMetricsDefault]; //设置背景图,背景色以及风格都无效
self.toolbarItems = items;  //设置本视图控制器在导航控制器中工具条上的显示item

//返回按钮的默认标题是Back

rvc.title = @"root"; //设置标题,当前视图的标题,也是下页返回按钮的标题
[self.navigationController pushViewController:新导航控制器对象 animated:YES]; //推入导航控制器   //view的背景色默认是透明色,推入导航控制器会有卡顿
[self.navigationController popViewControllerAnimated:YES];  //返回上一级,弹出顶层视图
[self.navigationController popToRootViewControllerAnimated:YES];  //返回到根视图控制器
BOOL hidden=!self.navigationController.navigationBarHidden //导航栏时候隐藏
[self.navigationController setNavigationBarHidden:hidden animated:YES]; //隐藏导航栏
self.navigationController.navigationBar.translucent = NO; //导航条设置为不透明
//取消导航对滚动视图的影响,不会影响布局
self.automaticallyAdjustsScrollViewInsets = NO;
//取消边界影响,同时会影响布局
self.edgesForExtendedLayout = UIRectEdgeNone;

//手势UITouch : UIViewController   day08
//滑动时一直调用,不能做耗时操作
- (void)touchesMoved:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event {
    [super touchesMoved:touches withEvent:event];
}
- (void)touchesEnded:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event {
    [super touchesEnded:touches withEvent:event];
    printf("结束\n");
}
- (void)touchesCancelled:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event {
    printf("取消\n");
}
//点击手势
UITapGestureRecognizer *tgr = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapHandle:)];
tgr.numberOfTapsRequired = 2; //设置点击次数
tgr.numberOfTouchesRequired = 2;  //触摸点的个数
[self.label addGestureRecognizer:tgr]; //添加手势
- (void)tapHandle:(UITapGestureRecognizer *)tgr {
    if (tgr.state ==UIGestureRecognizerStateBegan / UIGestureRecognizerStateEnded / UIGestureRecognizerStateChanged)//三种参数 开始 结束  状态改变
    {
        printf("点击手势识别成功\n");
    }
}
//长按手势
UILongPressGestureRecognizer *lpgr = [[UILongPressGestureRecognizer alloc] initWithTarget:self action:@selector(longPressHandle:)];
lpgr.minimumPressDuration = 2;//设置长按识别的最短持续时间
//滑动手势
UIPanGestureRecognizer *pgr = [[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(panHandle:)];
[pgr setTranslation:CGPointZero inView:self.view]; //偏移效果会累加,每次都需要清除
//捏合手势
UIPinchGestureRecognizer *pgr = [[UIPinchGestureRecognizer alloc] initWithTarget:self action:@selector(pinchHandle:)];
pgr.scale = 1.0; //捏合的比例效果会累加,每次都需重置
//旋转手势
UIRotationGestureRecognizer *rgr = [[UIRotationGestureRecognizer alloc] initWithTarget:self action:@selector(rotationHandle:)];
rgr.rotation = 0; //旋转角度效果会叠加,每次都需清除
//晃动手势
- (void)motionBegan:(UIEventSubtype)motion withEvent:(UIEvent *)event {
    printf("开始晃动\n");
}
- (void)motionEnded:(UIEventSubtype)motion withEvent:(UIEvent *)event {
    printf("晃动结束\n");
}
- (void)motionCancelled:(UIEventSubtype)motion withEvent:(UIEvent *)event {
    printf("晃动取消\n");
}
//轻扫手势,相当与具有单一方向的滑动手势
UISwipeGestureRecognizer *sgr = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(swipeHandle:)];
sgr.direction = dir[i]; //设置轻扫的方向
UISwipeGestureRecognizerDirection dir[] = {
        UISwipeGestureRecognizerDirectionRight,
        UISwipeGestureRecognizerDirectionLeft,
        UISwipeGestureRecognizerDirectionUp,
        UISwipeGestureRecognizerDirectionDown
    };
//音乐相关
先添加Audio 库  并添加 #import <AudioToolbox/AudioToolbox.h> 头文件
- (void)playMusicWithName:(NSString *)name { // 参数音乐文件名
    NSArray *names = [name componentsSeparatedByString:@"."];
    NSString *file = [[NSBundle mainBundle] pathForResource:names[0] ofType:names[1]];
    NSURL *url = [NSURL fileURLWithPath:file];

    SystemSoundID soundId;
    AudioServicesCreateSystemSoundID((CFURLRef)url, &soundId);
    AudioServicesPlayAlertSound(soundId);
}
UICommonlyUsedControls :UIViewController   day06
一、UISwitch(开关)
//可以通过设置形变进行缩放
sw.transform = CGAffineTransformMakeScale(2, 2);
//设置打开时的颜色,默认是绿色
sw.onTintColor = [UIColor redColor];
//设置关闭时的颜色(只有边框)
sw.tintColor = [UIColor yellowColor];
//设置小球的颜色
sw.thumbTintColor = [UIColor blackColor];
//点击时会系统会调用点击事件方法来改变开关的值
- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event
{
    [super touchesBegan:touches withEvent:event];
    
    UISwitch *sw = (UISwitch *)[self.view viewWithTag:100];
    BOOL status = !sw.isOn;
    [sw setOn:status animated:YES];
}
二、UIActivityIndicatorView(活动指示视图)
UIActivityIndicatorView *aiv = [[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleGray];//创建活动指示视图
//动画停止时是否隐藏,默认为YES
//aiv.hidesWhenStopped = NO;
[aiv startAnimating];
[UIApplication sharedApplication].networkActivityIndicatorVisible = NO; //状态栏的小图标的显示开关
三、UISlider(滑动条)
slider.maximumTrackTintColor = [UIColor greenColor];//设置轨道颜色
slider.minimumTrackTintColor = [UIColor redColor];//设置滑过轨道颜色
slider.thumbTintColor = [UIColor purpleColor];//滑块(小球)的颜色
//值的设置
slider.minimumValue = 0.2;
slider.maximumValue = 1;
slider.value = 1.0;
slider.continuous = NO;//拖拽期间是否一直回调处理函数
slider.minimumValueImage = image; //最小值贴图
slider.maximumValueImage = maxImage;  //最大值贴图
[slider setThumbImage:minImage forState:UIControlStateNormal]; //滑球正常状态时的图片
[slider setThumbImage:maxImage forState:UIControlStateHighlighted]; // 点击时的图片
self.view.alpha = slider.value;  // 通过 滑条的值 来改变 View 的透明度
四、UIProgressView(进度条)
 UIProgressView *pv = [[UIProgressView alloc] initWithProgressViewStyle:UIProgressViewStyleBar];风格创建
pv.progress = 0.5;
//设置颜色
pv.trackTintColor = [UIColor blackColor];
pv.progressTintColor = [UIColor greenColor];
五、UIStepper(步进器)
//设置值
stepper.minimumValue = 0;
stepper.maximumValue = 10;
stepper.stepValue = 1;
//跨越边界
stepper.wraps = YES;
//处理事件是否一直回调
stepper.continuous = NO;
//长按时是否一直重复操作
stepper.autorepeat = NO;
六、UISegmentedControl(分段控制)
//设置宽度,没有设置的段平均分
[sc setWidth:65 forSegmentAtIndex:1];
七、UIActionSheet(操作表单)
UIActionSheet *sheet = [[UIActionSheet alloc] initWithTitle:@"分享" delegate:self cancelButtonTitle:@"取消" destructiveButtonTitle:@"删除" otherButtonTitles:@"微博分享",@"微信分享",@"QQ分享", nil];//系统方法创建
//默认是隐藏 需要调用点击方法来创建显示
- (void)actionSheet:(UIActionSheet *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex
{    //这里做事件分发
    printf("index:%ld\n", buttonIndex);
    //使用代码方式取消显示
    [actionSheet dismissWithClickedButtonIndex:actionSheet.cancelButtonIndex animated:YES];
}
八、UIAlertView(警告视图)
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"⚠️" message:@"取钱请尽快,10秒后吞卡..." delegate:self cancelButtonTitle:@"不取了
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值