您的位置:首页 > 移动开发 > Swift

女神画廊App (Swift1.2)

2015-09-01 15:06 337 查看
这个App的是storyboard+code的结合,主要的重点是:

1.segue传递图片值。

2.Autolayout中可以使用右下角三角符号使用Add Missing Constraints进行大概约束,使之适应不同大小屏幕。

3.上边栏Editor -> Embed In -> Navigation Controller或者Tab Bar Controller。

4.实现分享功能。

5.注意storyboard设计Navigation Controller界面时关于segue的modal类型要改为push类型。

6.按住Command点击某个Controller控件代码可显示其方法类。

storyboard设计界面:



目录结构:



ViewController.swift文件代码:

import UIKit
class ViewController: UIViewController {

@IBOutlet weak var beautyPicker: UIPickerView!
let beauties = ["范冰冰","李冰冰","王菲","杨幂","周迅"]
override func viewDidLoad() {
super.viewDidLoad()
beautyPicker.dataSource = self
beautyPicker.delegate = self
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}

override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
if segue.identifier == "GoToGallery"{
let index = beautyPicker.selectedRowInComponent(0)
var imageName:String?
switch index{
case 0:
imageName = "fanbingbing"
case 1:
imageName = "libingbing"
case 2:
imageName = "wangfei"
case 3:
imageName = "yangmi"
case 4:
imageName = "zhouxun"
default:
imageName = nil

}
var vc = segue.destinationViewController as! GalleryController
vc.imageName = imageName
}
}
}


View.swift (ViewController.swift的额外扩展文件):

import UIKit

extension ViewController:UIPickerViewDataSource{
// returns the number of 'columns' to display.
func numberOfComponentsInPickerView(pickerView: UIPickerView) -> Int{
return 1
}
// returns the # of rows in each component..
func pickerView(pickerView: UIPickerView, numberOfRowsInComponent component: Int) -> Int{
return beauties.count
}
}
extension ViewController:UIPickerViewDelegate{
func pickerView(pickerView: UIPickerView, titleForRow row: Int, forComponent component: Int) -> String!{
return beauties[row]
}
}


GalleryController.swift文件代码:

import UIKit
 import Social

class GalleryController: UIViewController {
var imageName:String?

@IBOutlet weak var beautyImage: UIImageView!
override func viewDidLoad() {
super.viewDidLoad()
/*if (imageName != nil){
beautyImage.image = UIImage(named: imageName!)//修改前
}*/

if let name = imageName {
beautyImage.image = UIImage(named: name)
switch name{
case "fanbingbing":
navigationItem.title = "范冰冰"
case "libingbing":
navigationItem.title = "李冰冰"
case "wangfei":
navigationItem.title = "王菲"
case "yangmi":
navigationItem.title = "杨幂"
case "zhouxun":
navigationItem.title = "周迅"
default:
navigationItem.title = "女神画廊"
}
}
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}

     //分享按钮
@IBAction func shareTapped(sender: AnyObject) {
var controller: SLComposeViewController = SLComposeViewController(forServiceType: SLServiceTypeTencentWeibo)
controller.setInitialText("一起来玩女神画廊app吧,就在Github上")
controller.addImage(beautyImage.image)
self.presentViewController(controller, animated: true, completion: nil)
}
 }


KoreanViewController文件代码:

import UIKit
 import Social
 class KoreanViewController: UIViewController {
@IBOutlet weak var beautyImage: UIImageView!
override func viewDidLoad() {
super.viewDidLoad()
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
     //facebook分享
@IBAction func facebooTapped(sender: AnyObject) {
var controller: SLComposeViewController = SLComposeViewController(forServiceType: SLServiceTypeFacebook)
controller.setInitialText("一起来玩女神画廊app吧,就在Github上")
controller.addImage(beautyImage.image)
self.presentViewController(controller, animated: true, completion: nil)
}
//twitter分享
@IBAction func twitterTapped(sender: AnyObject) {
var controller: SLComposeViewController = SLComposeViewController(forServiceType: SLServiceTypeTwitter)
controller.setInitialText("一起来玩女神画廊app吧,就在Github上")
controller.addImage(beautyImage.image)
self.presentViewController(controller, animated: true, completion: nil)
}
//微博分享
@IBAction func WeiboTapped(sender: AnyObject) {
var controller: SLComposeViewController = SLComposeViewController(forServiceType: SLServiceTypeSinaWeibo)
controller.setInitialText("一起来玩女神画廊app吧,就在Github上")
controller.addImage(beautyImage.image)
self.presentViewController(controller, animated: true, completion: nil)
}
 }


运行结果:



github地址:https://github.com/AbelSu131/BeautyGallery
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: