落書きノート

ふと自分が気になった事を書いてます

Swift iOSアプリ タブブラウザ作成 途中経過

ここまで出来ました。まだ完全には出来てません。アニメーションもつけたいなぁ…。

//
//  ViewController.swift
//  ios_collectionview_browser2
//
//  Created by 川合弘敏 on 2017/07/25.
//  Copyright © 2017年 川合弘敏. All rights reserved.
//

import UIKit

class ViewController: UIViewController, UIWebViewDelegate, UISearchBarDelegate, UICollectionViewDelegate, UICollectionViewDataSource {
    @IBOutlet weak var searchBar: UISearchBar!
    @IBOutlet weak var showButton: UIButton!
    @IBOutlet weak var collectionView: UICollectionView!
    let searchEngineUrl = "https://www.google.co.jp/search?q="
    var originalUrl = "test"
    var count = 1
    override func viewDidLoad() {
        super.viewDidLoad()
        // Do any additional setup after loading the view, typically from a nib.
        self.collectionView.delegate = self
        self.searchBar.delegate = self
    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }

    @IBAction func tappedButton(_ sender: Any) {
        let flowLayout = UICollectionViewFlowLayout()
        let margin: CGFloat = 3.0
        flowLayout.itemSize = CGSize(width: 100.0, height: 100.0)
        flowLayout.minimumInteritemSpacing = margin
        flowLayout.minimumLineSpacing = margin
        flowLayout.sectionInset = UIEdgeInsets(top: margin, left: margin, bottom: margin, right: margin)
        self.collectionView.collectionViewLayout = flowLayout
    }

    func searchBarSearchButtonClicked(_ searchBar: UISearchBar) {
        originalUrl = searchEngineUrl + searchBar.text!
        self.showButton.setTitle(count.description, for: .normal)
        self.collectionView.reloadData()
    }
    
    func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        return count
    }
    
    func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
        let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "originalCell", for: indexPath) as! CollectionViewCell
        let reqUrl = URL(string: originalUrl)
        let req = URLRequest(url: reqUrl!)
        cell.webView.loadRequest(req)
        return cell
    }
}

f:id:kh2n:20170725095529p:plain