专栏名称: Cocoa开发者社区
CocoaChina苹果开发中文社区官方微信,提供教程资源、app推广营销、招聘、外包及培训信息、各类沙龙交流活动以及更多开发者服务。
目录
相关文章推荐
51好读  ›  专栏  ›  Cocoa开发者社区

iOS开发如何学习前端

Cocoa开发者社区  · 公众号  · ios  · 2016-09-02 08:04

正文

▲点击上方“CocoaChina”关注即可免费学习iOS开发


作者:叶孤城___

原文链接:http://www.jianshu.com/p/2d7a8e16615d


我为何学前端?

因为无聊


概念


前端大概三大块.

  • HTML

  • CSS

  • JavaScript


基本上每个概念在iOS中都有对应的.


HTML请想象成只能拉Autolayout或者设置Frame的ViewController.


好比你在网页上放了一个Button,如果用HTML你就可以设置他的摆放位置,在哪哪个控件里.但是你不可以设置大小,颜色,圆角之类的属性.


CSS专门负责HTML管不了的这些颜色啊,大小啊之类的属性.


JavaScript主要负责响应事件,你把它想象成iOS里面的处理Event就行了.


废话不多说.第一篇,我们做个导航条.


如图


展开你的想象力,在iOS做这样的一个导航条你会用哪个控件?


无非就是ScrollView或者TableView或者Collectionview.


其实HTML这种东西也叫作列表.对应的HTML标签叫做


我上面已经说过了,ul你可以看作是iOS中的UITableView.那么相应的,ul标签所包含的li标签你当然可以看做是一个个UITableViewCell. 所以,整个body标签包含的就是一个拥有6列的列表.(等同于一个拥有六个Cell的UITableView)

保存为index.html之后用chrome打开之后的效果是这样的.



效果和我们想要的还有一定差距.

  • 每一列之前有个黑点,我们不想要,应该去掉.

  • 导航栏应该是横的而非竖的.


当然还有很多细节不一样,但是这两个最明显,所以我们先改掉这两个问题.


CSS


既然涉及到样式的问题,那么这已经超出HTML力所能及的范围了.我们这时候就要引入CSS了.


怎么引入?


在index.html的同一个文件夹内创建style.css文件.


然后在我们的index.html的

标签下面加入这样一句话.<p style="white-space: normal; line-height: 25.6px;"><br/></p><p style="white-space: normal; line-height: 25.6px;"><link rel="stylesheet" href="./style.css"/></p><p style="white-space: normal; line-height: 25.6px;"><br/></p><p style="white-space: normal; line-height: 25.6px;">这样,就告诉了我们的HTML,在渲染整个网页的时候,样式之类的东西请在当前文件夹的style.css文件里找. OK。</p><p style="white-space: normal; line-height: 25.6px;"><br/></p><p style="white-space: normal; line-height: 25.6px;">该写CSS了.</p><p style="white-space: normal; line-height: 25.6px;"><br/></p><p style="white-space: normal; line-height: 25.6px;">其实所有GUI的属性都差不多.背景色一般叫backgroundColor, 间距一般叫padding, 文字对齐方式一般叫textAlignment.</p><p style="white-space: normal; line-height: 25.6px;"><br/></p><p style="white-space: normal; line-height: 25.6px;">诸如此类.</p><p style="white-space: normal; line-height: 25.6px;"><br/></p><p style="white-space: normal; line-height: 25.6px;">所以,我们的第一件事就是,把每一列之前的黑点去掉.</p><p style="white-space: normal; line-height: 25.6px;"><br/></p><p style="white-space: normal; line-height: 25.6px;">按照iOS的习惯,我们是不是应该先找到某个控件,再去修改控件的属性?</p><p style="white-space: normal; line-height: 25.6px;"><br/></p><p style="white-space: normal; line-height: 25.6px;">在CSS中也是一样.</p><p style="white-space: normal; line-height: 25.6px;"><br/></p><p style="white-space: normal; line-height: 25.6px;">怎么获取想要修改的控件?</p><p style="white-space: normal; line-height: 25.6px;"><br/></p><p style="white-space: normal; line-height: 25.6px;">很简单.这样就可以了.</p><p style="white-space: normal; line-height: 25.6px;"><br/></p><blockquote><p style="white-space: normal; line-height: 25.6px;">ul {</p><p style="white-space: normal; line-height: 25.6px;">list-style-type: none;</p><p style="white-space: normal; line-height: 25.6px;">}</p></blockquote><p style="white-space: normal; line-height: 25.6px;"><br/></p><p style="white-space: normal; line-height: 25.6px;">再运行一下看看.</p><p style="white-space: normal; line-height: 25.6px;"><br/></p><p style="text-align: center; line-height: 25.6px; white-space: normal;"><img class="imagebubble-image" data-ratio="0.32299270072992703" data-src="https://img.100weidu.com/mmbiz_png/foPACGrddJ0CH1G5cd36XgtI4VlmJIfeLpwOJeibncicyaxqhXbcPOibbWHvibMqmRqJZLUriaMbwu2SsNZYFZyoOFQ" data-type="png" data-w="1096"/></p><p style="white-space: normal; line-height: 25.6px;"><br/></p><p style="white-space: normal; line-height: 25.6px;">好的,第一个问题已经解决.</p><p style="white-space: normal; line-height: 25.6px;"><br/></p><p style="white-space: normal; line-height: 25.6px;">现在解决第二个问题.如何让</p><ul>标签中的每一条横着放.<p style="white-space: normal; line-height: 25.6px;"><br/></p><p style="white-space: normal; line-height: 25.6px;">其实也很简单.</p><p style="white-space: normal; line-height: 25.6px;"><br/></p><p style="white-space: normal; line-height: 25.6px;">我们只需要设置li 标签中的一个属性float就可以了.</p><p style="white-space: normal; line-height: 25.6px;"><br/></p><p style="white-space: normal; line-height: 25.6px;">这个东西可以理解为布局方向.</p><p style="white-space: normal; line-height: 25.6px;"><br/></p><p style="white-space: normal; line-height: 25.6px;">如果设置为这样就可以了.</p><p style="white-space: normal; line-height: 25.6px;"><br/></p><blockquote><p style="white-space: normal; line-height: 25.6px;"><span style="font-size: 14px;">li {</span></p><p style="white-space: normal; line-height: 25.6px;"><span style="font-size: 14px;">float: left;</span></p><p style="white-space: normal; line-height: 25.6px;"><span style="font-size: 14px;">}</span></p></blockquote><p style="white-space: normal; line-height: 25.6px;"><br/></p><p style="white-space: normal; line-height: 25.6px;">刷新一下chrome看看效果.</p><p style="text-align: center; line-height: 25.6px; white-space: normal;"><img class="imagebubble-image" data-ratio="0.13333333333333333" data-src="https://img.100weidu.com/mmbiz_png/foPACGrddJ0CH1G5cd36XgtI4VlmJIfeo5GhvMzs2naP04DLDaVgUria35mI2FyCZWhthUDEM2BWCjKXl6GFv4Q" data-type="png" data-w="1200"/></p><p style="white-space: normal; line-height: 25.6px;">已经横过来了.</p><p style="white-space: normal; line-height: 25.6px;"><br/></p><p style="white-space: normal; line-height: 25.6px;">接下来,其实就是设置各种属性.让我们的导航条看起来和w3schools.com的导航条看起来一样.</p><ul class=" list-paddingleft-2" style="list-style-type: disc;"><li><p style="white-space: normal; line-height: 25.6px;">去掉每一个<a>,也就是链接下面的下划线.</a></p></li><li><p style="white-space: normal; line-height: 25.6px;">选中状态的区别.</p></li><li><p style="white-space: normal; line-height: 25.6px;">ul标签的背景色.</p></li><li><p style="white-space: normal; line-height: 25.6px;">鼠标悬停的时候,每一列的背景色要发生变化.</p></li></ul><p style="white-space: normal; line-height: 25.6px;"><br/></p><p style="white-space: normal; line-height: 25.6px;">直接贴代码了.</p><p style="white-space: normal; line-height: 25.6px;"><br/></p><blockquote><p style="white-space: normal; line-height: 25.6px;"><span style="font-size: 14px;">ul {</span></p><p style="white-space: normal; line-height: 25.6px;"><span style="font-size: 14px;">list-style-type: none;</span></p><p style="white-space: normal; line-height: 25.6px;"><span style="font-size: 14px;">margin: 0;</span></p><p style="white-space: normal; line-height: 25.6px;"><span style="font-size: 14px;">padding: 0;</span></p><p style="white-space: normal; line-height: 25.6px;"><span style="font-size: 14px;">background: #5f5f5f;</span></p><p style="white-space: normal; line-height: 25.6px;"><span style="font-size: 14px;">height: 44px;</span></p><p style="white-space: normal; line-height: 25.6px;"><span style="font-size: 14px;">}</span></p><p style="white-space: normal; line-height: 25.6px;"><span style="font-size: 14px;">li {</span></p><p style="white-space: normal; line-height: 25.6px;"><span style="font-size: 14px;">float: left;</span></p><p style="white-space: normal; line-height: 25.6px;"><span style="font-size: 14px;">height: 44px;</span></p><p style="white-space: normal; line-height: 25.6px;"><span style="font-size: 14px;">width: auto;</span></p><p style="white-space: normal; line-height: 25.6px;"><span style="font-size: 14px;">}</span></p><p style="white-space: normal; line-height: 25.6px;"><span style="font-size: 14px;">li a {</span></p><p style="white-space: normal; line-height: 25.6px;"><span style="font-size: 14px;">display: block;</span></p><p style="white-space: normal; line-height: 25.6px;"><span style="font-size: 14px;">text-decoration: none;</span></p><p style="white-space: normal; line-height: 25.6px;"><span style="font-size: 14px;">color: white;</span></p><p style="white-space: normal; line-height: 25.6px;"><span style="font-size: 14px;">text-align: center;</span></p><p style="white-space: normal; line-height: 25.6px;"><span style="font-size: 14px;">padding: 14px 16px</span></p><p style="white-space: normal; line-height: 25.6px;"><span style="font-size: 14px;">}</span></p><p style="white-space: normal; line-height: 25.6px;"><span style="font-size: 14px;">li a:hover:not(.active) {</span></p><p style="white-space: normal; line-height: 25.6px;"><span style="font-size: 14px;">background-color: black;</span></p><p style="white-space: normal; line-height: 25.6px;"><span style="font-size: 14px;">}</span></p><p style="white-space: normal; line-height: 25.6px;"><span style="font-size: 14px;">.active {</span></p><p style="white-space: normal; line-height: 25.6px;"><span style="font-size: 14px;">background-color: #4CAF50;</span></p><p style="white-space: normal; line-height: 25.6px;"><span style="font-size: 14px;">}</span></p></blockquote><p style="white-space: normal; line-height: 25.6px;"><br/></p><p style="white-space: normal; line-height: 25.6px;">没学过CSS的看见这些代码估计有点晕.我来解释一下.</p><p style="white-space: normal; line-height: 25.6px;"><br/></p><p style="white-space: normal; line-height: 25.6px;"><strong>li a {}这种是什么意思?</strong></p><p style="white-space: normal; line-height: 25.6px;"><br/></p><p style="white-space: normal; line-height: 25.6px;">意思就是</p><li>标签里的所有<a>标签的属性.所以,只要有这种多个标签放一起中间用空格隔开的东西意思就是,右边的标签包含在左边的标签里.<p style="white-space: normal; line-height: 25.6px;"><br/></p><p style="white-space: normal; line-height: 25.6px;"><strong>.active 是什么意思?</strong></p><p style="white-space: normal; line-height: 25.6px;"><br/></p><p style="white-space: normal; line-height: 25.6px;">CSS里面有两个符号要记清楚一个是.一个是#,什么意思?</p><p style="white-space: normal; line-height: 25.6px;"><br/></p><p style="white-space: normal; line-height: 25.6px;">举个例子.</p><p style="white-space: normal; line-height: 25.6px;"><br/></p><blockquote><p style="white-space: normal; line-height: 25.6px;"/><li class="FistLi"/><p style="white-space: normal; line-height: 25.6px;"/><li class="SecondLi"/></blockquote><p style="white-space: normal; line-height: 25.6px;"><br/></p><p style="white-space: normal; line-height: 25.6px;">问,我现在需要把class为FistLi这个标签的背景色改为红色,把class为SecondLi的这个标签的背景色改为黄色,怎么改?</p><p style="white-space: normal; line-height: 25.6px;"><br/></p><p style="white-space: normal; line-height: 25.6px;">那么你就需要在CSS里这么写.</p><p style="white-space: normal; line-height: 25.6px;"><br/></p><blockquote><p style="white-space: normal; line-height: 25.6px;">li.FirstLi {</p><p style="white-space: normal; line-height: 25.6px;">background-color: red;</p><p style="white-space: normal; line-height: 25.6px;">}</p><p style="white-space: normal; line-height: 25.6px;">li.SecondLi {</p><p style="white-space: normal; line-height: 25.6px;">background-color: yellow;</p><p style="white-space: normal; line-height: 25.6px;">}</p></blockquote><p style="white-space: normal; line-height: 25.6px;"><br/></p><p style="white-space: normal; line-height: 25.6px;">是不是看出了点端倪.</p><p style="white-space: normal; line-height: 25.6px;"><br/></p><p style="white-space: normal; line-height: 25.6px;">这个.符号后面一般会跟某个标签的class属性的值.用来特指某一个标签.</p><p style="white-space: normal; line-height: 25.6px;"><br/></p><p style="white-space: normal; line-height: 25.6px;">想象一下,假如设计了一个特别复杂的页面.里面很多个不同的标签,那你如何区分?这时候需要给不同的标签设计不同的class或者id用以区分.简单来说,这玩意就像变量名.这么说应该懂了吧.</p><p style="white-space: normal; line-height: 25.6px;"><br/></p><p style="white-space: normal; line-height: 25.6px;">说完了.,那么#又是干什么的?</p><p style="white-space: normal; line-height: 25.6px;"><br/></p><p style="white-space: normal; line-height: 25.6px;">功能类似.不过.是用来选择class这个属性的,而#是用来选择id这个属性的.</p><p style="white-space: normal; line-height: 25.6px;"><br/></p><p style="white-space: normal; line-height: 25.6px;">我们只需要把上述的例子换成这样.也能达到相同的效果.</p><p style="white-space: normal; line-height: 25.6px;"><br/></p><p style="white-space: normal; line-height: 25.6px;">在HTML中.</p><blockquote><p style="white-space: normal; line-height: 25.6px;"/><li id="FistLi"/><p style="white-space: normal; line-height: 25.6px;"/><li id="SecondLi"/></blockquote><p style="white-space: normal; line-height: 25.6px;"><br/></p><p style="white-space: normal; line-height: 25.6px;">在CSS中.</p><blockquote><p style="white-space: normal; line-height: 25.6px;">li#FirstLi {</p><p style="white-space: normal; line-height: 25.6px;">background-color: red;</p><p style="white-space: normal; line-height: 25.6px;">}</p><p style="white-space: normal; line-height: 25.6px;">li#SecondLi {</p><p style="white-space: normal; line-height: 25.6px;">background-color: yellow;</p><p style="white-space: normal; line-height: 25.6px;">}</p></blockquote><p style="white-space: normal; line-height: 25.6px;"><br/></p><p style="white-space: normal; line-height: 25.6px;">那你估计要问了.这两个功能差不多啊.那我到底该用哪个?</p><p style="white-space: normal; line-height: 25.6px;"><br/></p><p style="white-space: normal; line-height: 25.6px;">引入官方解释.</p><p style="white-space: normal; line-height: 25.6px;"><br/></p><blockquote><p style="white-space: normal; line-height: 25.6px;">据说W3C对于ID和CLASS的设定是ID具有唯一性,class具有普遍性。所以说我们用他们俩的时候就要按照这个特性来使用。id 是不能重复的,class 却是可以重复使用,通过id可以找到页面上唯一的一个标签,而class呢可以多个标签使用同一种样式提供了可能</p></blockquote><p style="white-space: normal; line-height: 25.6px;"><br/></p><p style="white-space: normal; line-height: 25.6px;">li a:hover:not(.active)这么一长串又是个什么鬼?</p><p style="white-space: normal; line-height: 25.6px;"><br/></p><p style="white-space: normal; line-height: 25.6px;">咱们一点一点分析.</p><p style="white-space: normal; line-height: 25.6px;"><br/></p><p style="white-space: normal; line-height: 25.6px;">首先li a这个的意思是在</p><li>里面的<a>标签.<p style="white-space: normal; line-height: 25.6px;"><br/></p><p style="white-space: normal; line-height: 25.6px;">a:hover,hover是英文盘旋的意思.</p><p style="white-space: normal; line-height: 25.6px;"><br/></p><p style="white-space: normal; line-height: 25.6px;">这句什么意思呢?</p><p style="white-space: normal; line-height: 25.6px;"><br/></p><p style="white-space: normal; line-height: 25.6px;">因为a代表的是一个超链接.a:hover的意思就是,当鼠标停留在这个超链接上.</p><p style="white-space: normal; line-height: 25.6px;"><br/></p><p style="white-space: normal; line-height: 25.6px;">所以前半句的意思是,当用户的鼠标停留在li标签里的a标签上的时候.</p><p style="white-space: normal; line-height: 25.6px;"><br/></p><p style="white-space: normal; line-height: 25.6px;">后面又接了一个:not(.active).</p><p style="white-space: normal; line-height: 25.6px;"><br/></p><p style="white-space: normal; line-height: 25.6px;">根据前面的解释.active意思就是,class等于active的所有标签.前面加个not什么意思?就是所有class不是active的标签.</p><p style="white-space: normal; line-height: 25.6px;"><br/></p><p style="white-space: normal; line-height: 25.6px;">好了,现在连起来. 朗读一遍.</p><p style="white-space: normal; line-height: 25.6px;"><br/></p><p style="white-space: normal; line-height: 25.6px;">当鼠标停在所有li标签里的a标签,但是a得class属性又不等于active的时候请执行下面的css.</p><p style="white-space: normal; line-height: 25.6px;"><br/></p><p style="white-space: normal; line-height: 25.6px;">就是这样</p><p style="white-space: normal; line-height: 25.6px;"><br/></p><p style="white-space: normal; line-height: 25.6px;">最后,大家按照上面的自己写一遍吧.</p><p style="white-space: normal; line-height: 25.6px;"><br/></p><section class="_135editor" style="box-sizing: border-box;" data-id="87417" data-tools="135编辑器"><p style="text-align: center; white-space: normal;"><img title="音符" style="display: inline;" data-ratio="0.45454545454545453" data-w="22" data-src="https://img.100weidu.com/mmbiz/cZV2hRpuAPiaJQXWGyC9wrUzIicibgXayrgibTYarT3A1yzttbtaO0JlV21wMqroGYT3QtPq2C7HMYsvicSB2p7dTBg"/></p><p style="white-space: normal;"><br/></p></section><p style="white-space: normal; line-height: 25.6px; color: rgb(62, 62, 62);"><a style="box-sizing: border-box;" href="http://promotion.xitu.io/2016-exam" target="_blank" data_ue_src="http://promotion.xitu.io/2016-exam"><img width="auto" style="visibility: visible !important; width: auto !important;" data-ratio="0.55625" data-w="640" data-s="300,640" data-type="jpeg" data-src="https://img.100weidu.com/mmbiz_jpg/foPACGrddJ1wiajbUpXPWibzprQYJDKBmB8kaFOibw88hkKB4gc9VrqdX0mDray0ldgoeA8e3UJNs07OnIOXADKeA"/></a><br/><br/></p><p style="white-space: normal; line-height: 25.6px; color: rgb(62, 62, 62);"><span style="font-size: 14px;">最近发现一个好玩的测试,互联网从业者都应该来试下,我得了 85 分,看看你能不能超过我!长按图片识别二维码或者点击<a style="box-sizing: border-box;" href="http://promotion.xitu.io/2016-exam" target="_blank" data_ue_src="http://promotion.xitu.io/2016-exam">阅读原文</a>就可以参与。</span></p><p style="line-height: 25.6px; color: rgb(62, 62, 62); white-space: normal;"><br/></p><hr style="line-height: 25.6px; white-space: normal; color: rgb(62, 62, 62); box-sizing: border-box;"/><p style="line-height: 25.6px; color: rgb(62, 62, 62); text-align: center; white-space: normal;">微信号:<strong><span style="font-size: 14px; color: #0070C0;">CocoaChinabbs</span></strong></p><p style="line-height: 25.6px; color: rgb(62, 62, 62); text-align: center; white-space: normal;"><img width="137" style="visibility: visible !important; width: 137px !important;" data-ratio="0.9927007299270073" data-w="137" data-s="300,640" data-type="jpeg" data-src="https://img.100weidu.com/mmbiz/foPACGrddJ2Udhpia6PsaSdDwtNNS3uS9oicNswCiaZ831emagAheicAAWic4sWszUo1ibxEbic1gibmPCiceMkoymgXfZA"/><br/><strong><span style="color: #FF4C41; font-family: 微软雅黑, sans-serif; font-size: 14px; line-height: 19px;">▲长按二维码“识别”关注<strong style="line-height: 25.6px;">即可免费学习 iOS 开发</strong></span></strong></p><p style="line-height: 25.6px; color: rgb(62, 62, 62); text-align: center; white-space: normal;"><span style="color: #FF4C41; font-family: 微软雅黑, sans-serif; font-size: 14px;"><strong>月薪十万、出任CEO、赢娶白富美、走上人生巅峰不是梦</strong></span></p><p style="line-height: 25.6px; color: rgb(62, 62, 62); text-align: center; white-space: normal;"><span style="font-size: 14px;"><strong>--------<strong>-----------------</strong><strong>-------------</strong></strong></span></p><p style="line-height: 25.6px; color: rgb(62, 62, 62); text-align: center; white-space: normal;"><span style="font-size: 14px;">商务合作QQ:2408167315</span></p><p style="line-height: 25.6px; color: rgb(62, 62, 62); text-align: center; white-space: normal;"><span style="font-size: 14px;">投稿邮箱:support@cocoachina.com</span></p></a></li></a></li></ul>