iOS学习
- 前言
- 发现:控件的堆叠
- 我的:更换头像
- 账号:切换夜间
- 总结
前言
本周正式开始暑期学习,完成了网易云音乐的仿写。
发现:控件的堆叠
发现页面由一个无限轮播图,四个自定义cell完成。无限轮播图在之前已经详细解释过。此处给出一个按钮的configuration属性的学习。
当需要文字和图片同时出现在一个按钮中时,我们可以采取settitle再setimage两个方法。但是文字会被初始化为白色,因此要修改文字颜色使文字显现。除此之外,我们还可以使用按钮的configuration来创建一个标题图像按钮。
UIButtonConfiguration是一种装饰模式,仅对装饰响应,不会对手势,消息传递等动作做响应。使用该属性我们需要先定义一个属性并且初始化
UIButtonConfiguration *config = [UIButtonConfiguration plainButtonConfiguration];
下面给出具体使用方法:
for (int i = 0 ; i < 7; i++) {
UIButtonConfiguration *config = [UIButtonConfiguration plainButtonConfiguration];
NSString *str = array[i];
NSString *strImage = [NSString stringWithFormat:@"%@.png",str];
//为按钮设置标题,还可设置副标题,attritubedSubTitled。
config.attributedTitle = [[NSAttributedString alloc] initWithString:str];
config.image = [UIImage imageNamed:strImage];
//设置图片与文字的格式,此处为文字在下图片在上。
//leading:文字在右图像在上
//trailing:文字在左图像在右
//bottom:文字在上图像在下
config.imagePlacement = NSDirectionalRectEdgeTop;
config.buttonSize = UIButtonConfigurationSizeMini;
//设置图文间隙
config.imagePadding = 8;
config.baseForegroundColor = [UIColor blackColor];
config.imageColorTransformer = ^UIColor * _Nonnull(UIColor * _Nonnull color) {
return [UIColor systemRedColor];};
_btn = [UIButton buttonWithType:UIButtonTypeCustom];
_btn.configuration = config;
}
这里仅对该属性做了简单的使用学习,想要更深入的理解还设计了很多没学过的属性。故此处给出学长博客供一起学习:按钮configuration学习博客
下面的则是在自定义cell中滚动轮播图和各种控件的结合,不作赘述。
我的:更换头像
该页面主要是通过协议传值更换头像的学习,此外还有分栏控件控制自定义cell中的滚动视图。
在同一个页面控制分栏控件和滚动视图我们学习过,此处大同小异,只需要将自定义cell中的滚动视图调用出来即可。采取定位cell的位置定义一个cell来控制其中的控件。
- (void)segmentedControlValueChanged:(UISegmentedControl *)_segControl {
NSIndexPath *indexPath = [NSIndexPath indexPathForRow:0 inSection:3];
myCell2_4 *cell = [self.tableView0 cellForRowAtIndexPath:indexPath];
[cell.scrollView0 setContentOffset:CGPointMake(_segControl.selectedSegmentIndex * cell.scrollView0.frame.size.width, 0) animated:YES];
cell.scrollView0.backgroundColor = [UIColor clearColor];
}
- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView {
if ([scrollView isKindOfClass:[UIScrollView class]]) {
NSInteger pageIndex = scrollView.contentOffset.x / scrollView.frame.size.width;
_segControl.selectedSegmentIndex = pageIndex;
}
}
换头像比较重点的就是使用协议传值,此处我传的是头像名称的字符串,也可以直接穿头像的image。同时要加上一次只能选择一个头像,选多了会判错。只需要设置一个属性变量,每当选择一个时就给count++,判断count是否为一。给出判断代码:
-(void) pressButton:(UIButton *) button{
button.selected = !button.selected;
if (button.selected) {
self.selectPhotoCount++;
} else {
self.selectPhotoCount--;
}
}
-(void) press{
if (self.selectPhotoCount == 1) {
for (UIView *subview in self.sv.subviews) {
if ([subview isKindOfClass:[UIButton class]]) {
UIButton *button = (UIButton *) subview;
if (button.selected) {
_strImageSend = [NSString stringWithFormat:@"头像%ld.jpg",(long)(button.tag - 100)];
if ([self.delegate respondsToSelector:@selector(didSelectImageWithNameAgain:)]) {
[self.delegate didSelectImageWithNameAgain:_strImageSend];
}
button.selected = NO;
[self.navigationController popToRootViewControllerAnimated:YES];
break;
}
}
}
} else {
_alertVier = [UIAlertController alertControllerWithTitle:@"警告" message:@"只能选择一张图片" preferredStyle:UIAlertControllerStyleAlert];
UIAlertAction* action01 = [UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDefault handler:^(UIAlertAction *action){/* 在这里编写执行该选项的代码*/}];
[_alertVier addAction:action01];
[self presentViewController:_alertVier animated:YES completion:nil];
}
}
关于传值问题,待深入学习理解后,单独写一篇博客总结。
账号:切换夜间
先给出效果图:
夜间模式主要是通过传值,在Switch不同状态时传出不同的值,然后对背景颜色进行更改,此处给出一种思路:
在switch控件中:
- (void) swChange:(UISwitch*) sw
{
_dictionary = [NSMutableDictionary dictionary];
_isNightMode1 = sw.isOn;
if (_isNightMode1) {
[_dictionary setValue:@"night" forKey:@"key"];
[[NSNotificationCenter defaultCenter] postNotificationName:@"notice" object:nil userInfo:_dictionary];
_label2.textColor = [UIColor whiteColor];
} else {
[_dictionary setValue:@"white" forKey:@"key"];
[[NSNotificationCenter defaultCenter] postNotificationName:@"notice" object:nil userInfo:_dictionary];
_label2.textColor = [UIColor blackColor];
}
}
在视图控制器中:
-(void) receiveNotice:(NSNotification *)send {
self.strflag = send.userInfo[@"key"];
if ([self.strflag isEqual: @"night"]) {
self.tableView.backgroundColor = [UIColor darkGrayColor];
self.tabBarController.tabBar.backgroundColor = [UIColor systemBlueColor];
self.tabBarController.tabBar.barTintColor = [UIColor systemBlueColor];
self.tabBarController.tabBar.tintColor = [UIColor systemBlueColor];
} else {
self.tableView.backgroundColor = [UIColor whiteColor];
self.tabBarController.tabBar.backgroundColor = [UIColor systemBlueColor];
self.tabBarController.tabBar.barTintColor = [UIColor systemBlueColor];
self.tabBarController.tabBar.tintColor = [UIColor systemBlueColor];
}
[_tableView reloadData];
}
要将变量传递到其他视图中,则需要使用的通知传值。后续在博客中总结吧。
总结
网易云音乐的仿写收获颇丰。无论是对传值的学习还是对控件的使用,明显的感受到了自己的进步。继续努力!