iOS UIPageControl和UISegmentedControl控件

1. UIPageControl控件

UIPageControl控件提供一行点来提示当前显示的多个页面中的哪一页。

1.1 主要属性

// 小白点的数量
@property (nonatomic, assign) NSInteger numberOfPages;
// 当前选中的点
@property (nonatomic, assign) NSInteger currentPage;

// 未选中小白点的颜色
@property (nullable, nonatomic, strong) UIColor *pageIndicatorTintColor;
// 当前选中小白点的颜色
@property (nullable, nonatomic, strong) UIColor *currentPageIndicatorTintColor;

// 只有一个页面时隐藏控件,默认是NO
@property (nonatomic) BOOL hidesForSinglePage;

设置点的数量和颜色,

UIPageControl *pageControl = [[UIPageControl alloc] initWithFrame:rect];
pageControl.pageIndicatorTintColor = [UIColor grayColor];
pageControl.currentPageIndicatorTintColor = [UIColor magentaColor];
pageControl.numberOfPages = 6;
pageControl.currentPage = 2;

显示如下
在这里插入图片描述

1.2 iOS14新特性

背景样式UIPageControlBackgroundStyle

typedef NS_ENUM(NSInteger, UIPageControlBackgroundStyle) {
    /// The default background style that adapts based on the current interaction state.
    UIPageControlBackgroundStyleAutomatic  = 0,
    /// The background style that shows a full background regardless of the interaction
    UIPageControlBackgroundStyleProminent  = 1,
    /// The background style that shows a minimal background regardless of the interaction
    UIPageControlBackgroundStyleMinimal    = 2,
}

UIPageControlBackgroundStyleProminent为控件添加背景,显示如下
在这里插入图片描述

交互方式UIPageControlInteractionState

typedef NS_ENUM(NSInteger, UIPageControlInteractionState) {
    /// The default interaction state, where no interaction has occured.
    UIPageControlInteractionStateNone        = 0,
    /// The interaction state for which the page was changed via a single, discrete interaction.
    UIPageControlInteractionStateDiscrete    = 1,
    /// The interaction state for which the page was changed via a continuous interaction.
    UIPageControlInteractionStateContinuous  = 2,
}

交互方式有两种离散和连续,通过设置属性allowsContinuousInteraction修改,默认是YES

离散(UIPageControlInteractionStateDiscrete),每次只能移动一格
在这里插入图片描述

连续(UIPageControlInteractionStateContinuous),可以通过拖拽快速移动
在这里插入图片描述

自定义样式

UIPageControl还可以自定义小圆点,使用图片替代

属性preferredIndicatorImage可以自定义图片
在这里插入图片描述

或者通过setIndicatorImage: forPage:方法对不同位置的自定义图片
在这里插入图片描述

1.3 监听点击事件

[PageControl addTarget:self action:@selector(onPageControlValueChange:) 
        forControlEvents:UIControlEventValueChanged];

onPageControlValueChange:方法监听

- (void)onPageControlValueChange:(UIPageControl *)sender {
    NSLog(@"currentPage = %ld", sender.currentPage);
}

2. UISegmentedControl控件

UISegmentedControl控件用于界面之间的逻辑切换。

2.1 主要属性

// 默认选择项
@property(nonatomic) NSInteger selectedSegmentIndex;
// 选中项背景色
@property(nullable, nonatomic, strong) UIColor *selectedSegmentTintColor;

// 选中后是否自动释放,默认NO
@property(nonatomic,getter=isMomentary) BOOL momentary; 
// 是否根据内容定分段宽度,默认是NO
@property(nonatomic) BOOL apportionsSegmentWidthsByContent;
// 选项数量
@property(nonatomic,readonly) NSUInteger numberOfSegments;

示例代码

NSArray *titleArray = @[@"东", @"南", @"西", @"北", @"这个选项有点长"];

UISegmentedControl *normalSegmentedControl = [[UISegmentedControl alloc] initWithItems:titleArray];
normalSegmentedControl.frame = CGRectMake(10, 100, [UIScreen mainScreen].bounds.size.width-20 , 44);
normalSegmentedControl.selectedSegmentIndex = 1;
[self.view addSubview:normalSegmentedControl];

UISegmentedControl *selectSegmentedControl = [[UISegmentedControl alloc] initWithItems:titleArray];
selectSegmentedControl.frame = CGRectMake(10, 180, [UIScreen mainScreen].bounds.size.width-20 , 44);
selectSegmentedControl.selectedSegmentIndex = 1;
selectSegmentedControl.selectedSegmentTintColor = [UIColor magentaColor];
[self.view addSubview:selectSegmentedControl];

UISegmentedControl *momentarySegmentedControl = [[UISegmentedControl alloc] initWithItems:titleArray];
momentarySegmentedControl.frame = CGRectMake(10, 260, [UIScreen mainScreen].bounds.size.width-20 , 44);
momentarySegmentedControl.momentary = YES;
[self.view addSubview:momentarySegmentedControl];

UISegmentedControl *apportionsSegmentedControl = [[UISegmentedControl alloc] initWithItems:titleArray];
apportionsSegmentedControl.frame = CGRectMake(10, 340, [UIScreen mainScreen].bounds.size.width-20 , 44);
apportionsSegmentedControl.selectedSegmentIndex = 1;
apportionsSegmentedControl.apportionsSegmentWidthsByContent = YES;
[self.view addSubview:apportionsSegmentedControl];

显示如下
在这里插入图片描述

2.2 主要方法

// 在指定索引设置标题
- (void)setTitle:(nullable NSString *)title forSegmentAtIndex:(NSUInteger)segment;
// 在指定索引设置图片
- (void)setImage:(nullable UIImage *)image forSegmentAtIndex:(NSUInteger)segment;
// 在指定索引设置宽度
- (void)setWidth:(CGFloat)width forSegmentAtIndex:(NSUInteger)segment;
// 在指定索引设置偏移
- (void)setContentOffset:(CGSize)offset forSegmentAtIndex:(NSUInteger)segment;

// 设置背景色
- (void)setBackgroundImage:(nullable UIImage *)backgroundImage forState:(UIControlState)state barMetrics:(UIBarMetrics)barMetrics;
// 设置分割线
- (void)setDividerImage:(nullable UIImage *)dividerImage forLeftSegmentState:(UIControlState)leftState rightSegmentState:(UIControlState)rightState barMetrics:(UIBarMetrics)barMetrics;

// 设置标题的文本属性
- (void)setTitleTextAttributes:(nullable NSDictionary<NSAttributedStringKey,id> *)attributes forState:(UIControlState)state;

示例代码

NSArray *titleArray = @[@"东", @"南", @"西", @"北", @"这个选项有点长"];

UISegmentedControl *normalSegmentedControl = [[UISegmentedControl alloc] initWithItems:titleArray];
normalSegmentedControl.frame = CGRectMake(10, 100, [UIScreen mainScreen].bounds.size.width-20 , 44);
[self.view addSubview:normalSegmentedControl];

[normalSegmentedControl setTitle:@"选项1" forSegmentAtIndex:0];
[normalSegmentedControl setImage:[UIImage imageNamed:@"icon_money"] forSegmentAtIndex:1];
[normalSegmentedControl setContentOffset:CGSizeMake(10, 10) forSegmentAtIndex:2];
[normalSegmentedControl setWidth:120 forSegmentAtIndex:4];

[normalSegmentedControl setBackgroundImage:[[UIColor whiteColor] colorToImage]
            forState:UIControlStateNormal barMetrics:UIBarMetricsDefault];
[normalSegmentedControl setBackgroundImage:[[UIColor blueColor] colorToImage]
            forState:UIControlStateSelected barMetrics:UIBarMetricsDefault];
[normalSegmentedControl setDividerImage:[[UIColor blueColor] colorToImage]
            forLeftSegmentState:UIControlStateNormal rightSegmentState:UIControlStateNormal
            barMetrics:UIBarMetricsDefault];
[normalSegmentedControl setTitleTextAttributes:@{NSForegroundColorAttributeName : [UIColor blueColor]}
            forState:UIControlStateNormal];
[normalSegmentedControl setTitleTextAttributes:@{NSForegroundColorAttributeName : [UIColor whiteColor]}
            forState:UIControlStateSelected];

normalSegmentedControl.layer.borderWidth = 1;
normalSegmentedControl.layer.borderColor = [UIColor blueColor].CGColor;

normalSegmentedControl.selectedSegmentIndex = 0;

显示如下
在这里插入图片描述
添加删除选项

// 在指定索引插入一个选项并设置题目
- (void)insertSegmentWithTitle:(nullable NSString *)title atIndex:(NSUInteger)segment animated:(BOOL)animated;
// 在指定索引插入一个选项并设置图片
- (void)insertSegmentWithImage:(nullable UIImage *)image atIndex:(NSUInteger)segment animated:(BOOL)animated;

// 删除指定索引选项
- (void)removeSegmentAtIndex:(NSUInteger)segment animated:(BOOL)animated;
// 删除所有选项
- (void)removeAllSegments;

2.3 监听点击事件

[UISegmentedControl addTarget:self action:@selector(onSegmentedControlValueChange:) 
        forControlEvents:UIControlEventValueChanged];
}

onSegmentedControlValueChange:方法监听

- (void)onSegmentedControlValueChange:(UISegmentedControl *)sender {
    NSLog(@"selectedSegmentIndex = %ld", sender.selectedSegmentIndex);
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
iOS中,要实现仿今日头条滑块滑动效果,可以借助UIKit框架中的UIScrollView以及UICollectionView来实现。 首先,在界面中添加一个UIScrollView,设置其frame为所需大小,并将其contentSize设置为所有滑块所占的总宽度。如果滑块数目较多,可以使用UICollectionView来更方便地布局和管理滑块。 接下来,可以使用UIPageControl来显示当前滑块的页数,通过与UIScrollView的滑动距离进行关联,使UIPageControl随着滑块的滑动而改变页数。可以通过UIScrollViewDelegate的方法scrollViewDidScroll来实现这一功能,根据当前的contentOffset来计算并更新UIPageControl的当前页数。 另外,要实现滑块的点击切换功能,可以使用UITapGestureRecognizer来监听滑块的点击事件。当检测到点击时,通过计算点击点的位置来判断点击的是哪个滑块,并使用UIScrollView的方法scrollRectToVisible来将该滑块滑动到可见区域。 为了增强滑块的滑动体验,可以结合UIView动画来实现平滑滑动的效果。使用UIView的方法animateWithDuration:animations:completion:来执行滑块的滑动动画,通过改变UIScrollView的contentOffset来实现滑块的平滑滑动。 此外,还可以添加一些其他效果来增强用户体验,例如滑块滑动到边缘时的弹性效果、滑块的缩放效果等。 总而言之,要实现iOS仿今日头条滑块滑动效果,需要借助UIScrollView或UICollectionView来布局和管理滑块,结合UIPageControl、UITapGestureRecognizer和UIView动画来实现滑块的滑动、点击和平滑滑动效果,并可以添加一些其他效果来增强用户体验。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值