iOS实现微信朋友圈与摇一摇功能
程序员文章站
2023-12-16 19:11:46
本demo为练手小项目,主要是熟悉目前主流app的架构模式.此项目中采用mvc设计模式,纯代码和少许xib方式实现.主要实现了朋友圈功能和摇一摇功能.
预览效果:...
本demo为练手小项目,主要是熟悉目前主流app的架构模式.此项目中采用mvc设计模式,纯代码和少许xib方式实现.主要实现了朋友圈功能和摇一摇功能.
预览效果:
主要重点
1.整体架构
利用uitabbarcontroller和uinavigationcontroller配合实现.其中要注意定义基类,方便整体上的管理,例如对uinavigationcontroller头部的颜色,字体和渲染颜色等设置.以及对uitabbarcontroller的底部的渲染等.
[self.navigationbarsetbackgroundimage:[uiimageimagenamed:@"dimensional-_code_bg"]forbarmetrics:uibarmetricsdefault]; [self.navigationbarsettitletextattributes:@{ nsforegroundcolorattributename:[uicolor whitecolor] }]; [self.navigationbarsettintcolor:[uicolor whitecolor]];
2.发现界面和我的界面
利用uitableviewcontroller和plist文件实现界面的展示.实现过程中有采用数据模型或直接利用字典等方式.这里的实现比较简单,就不多说啦.
- (instancetype)initwithdict:(nsdictionary *)dict{ if (self = [super init]) { [selfsetvaluesforkeyswithdictionary:dict]; } return self; } + (instancetype)picturewithdict:(nsdictionary *)dict{ return [[self alloc]initwithdict:dict]; }
3.朋友圈功能的实现
这里面主要的难点在于朋友圈首页的下拉刷新效果的实现,和选择照片页的状态重用问题,以及照片的传递和代理的实现等.
朋友圈首页的下拉刷新效果:主要利用transform属性和scrollview的多种滚动状态.
- (void)scrollviewwillbegindragging:(uiscrollview *)scrollview{ self.dragging = yes; } - (void)scrollviewdidscroll:(uiscrollview *)scrollview{ if (self.num == 0) { self.num ++; return; } cgfloat offsety = scrollview.contentoffset.y; cgfloat angle = -offsety* m_pi / 30; if (self.dragging == yes) { if (offsety <= 110) { self.containerview.y = 10 + offsety; } }else { if (self.currenty < 120) { self.containerview.y = 10 + offsety; } } self.activityview.transform = cgaffinetransformmakerotation(angle); } - (void)scrollviewdidenddragging:(uiscrollview *)scrollviewwilldecelerate:(bool)decelerate{ self.dragging = no; cgfloat currenty = self.containerview.y; self.currenty = currenty; } - (void)scrollviewdidenddecelerating:(uiscrollview *)scrollview{ [uiviewanimatewithduration:0.25animations:^{ self.containerview.frame = cgrectmake(15, 120, 30, 30); self.activityview.transform = cgaffinetransformmakerotation(2 * m_pi); }]; }
其中照片的展示是采用uicollectionviewcontroller来实现的.没有直接调用系统的相册,因此加大了难度.自定义了cell,并采用了代理方式来实现类与类之间的通信.
@protocol yypicturecelldelegate @optional - (void)picturecell:(yypicturecell *)cellwithdidclickbtn:(uibutton *)btn; @end - (ibaction)clicksurebtn:(uibutton *)sender { if ([self.delegaterespondstoselector:@selector(picturecell:withdidclickbtn:)]) { [self.delegatepicturecell:selfwithdidclickbtn:sender]; } } - (void)picturecell:(yypicturecell *)cellwithdidclickbtn:(uibutton *)btn{ if ((self.selectedbtn.count == 9) && (!btn.isselected)) { uialertview *alert = [[uialertview alloc]initwithtitle:nilmessage:@"最多选取9张照片哦,亲!"delegate:nilcancelbuttontitle:@"确定"otherbuttontitles: nil]; [alertshow]; return; } btn.selected = !btn.isselected; nsindexpath *indexpath = [self.collectionviewindexpathforcell:cell]; yypicturemodel *model = self.dataarray[indexpath.row]; if (btn.isselected) { model.clickedbtn = yes; [self.selectedbtnaddobject:btn]; [self.selimagearrayaddobject:model]; } else{ model.clickedbtn = no; [self.selectedbtnremoveobject:btn]; [self.selimagearrayremoveobject:model]; } if (self.selectedbtn.count > 0) { self.donebtn.enabled = yes; self.donebtn.selected = yes; self.previewbtn.enabled = yes; self.previewbtn.selected = yes; }else { self.donebtn.enabled = no; self.donebtn.selected = no; self.previewbtn.enabled = no; self.previewbtn.selected = no; } }
4.摇一摇功能的实现
摇一摇功能的本身实现十分简单,就是调用系统的两个方法即可.难点在于动画效果.其实这里的动画效果也不是很难.主要是计算有点复杂.可能是我在网上搜索到的素材有点不合适.导致要考虑各个控件的frame问题…
//实现摇一摇功能 - (void)motionbegan:(uieventsubtype)motionwithevent:(uievent *)event{ self.upline.hidden = no; self.downline.hidden = no; [uiviewanimatewithduration:0.6animations:^{ self.upimageview.y -= 60; self.upline.y -= 60; self.downimageview.y += 60; self.downline.y += 60; }completion:^(bool finished) { [uiviewanimatewithduration:0.6animations:^{ self.upimageview.y += 60; self.upline.y += 60; self.downimageview.y -= 60; self.downline.y -= 60; }completion:^(bool finished) { self.upline.hidden = yes; self.downline.hidden = yes; //弹出搜索框 [self showsearchview]; [self.searchviewperformselector:@selector(removefromsuperview)withobject:nilafterdelay:2.4]; }]; }]; } //摇一摇结束后 - (void)motionended:(uieventsubtype)motionwithevent:(uievent *)event{ }
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。