视图随着延迟迅速而消失

时间:2017-03-26 16:59:51

标签: ios swift xcode navigationcontroller icarousel

我有navigationController和2 UIViewControllers

当我"back button" secondViewController disappears视图时,1秒delay

P.S。我使用iCarousel窗格初始化secondViewController中的观看次数。

查看屏幕截图:

当我从另一个按下“后退”按钮时:

enter image description here

1秒后的FirstController(第二个控制器的视图消失)

enter image description here

更新 第二个ViewController

class AppsController : UIViewController, iCarouselDataSource, iCarouselDelegate {
let xmlHelper = XmlHelper()
var apps = Apps(data:[App]())
var selectUrl = ""
var selectTitle = ""
var scrollIndex = 0

@IBOutlet var carousel: iCarousel!

override func viewDidLoad() {
    super.viewDidLoad()
    self.carousel.delegate = self
    self.carousel.isPagingEnabled = true
    DispatchQueue.main.async {
        self.initApps()
    }
}

func numberOfItems(in carousel: iCarousel) -> Int {
    return apps.data.count
}

func carousel(_ carousel: iCarousel, viewForItemAt index: Int, reusing view: UIView?) -> UIView {
    let appView: AppView = Bundle.main.loadNibNamed("appView",
                                                    owner: nil,
                                                    options: nil)?.first as! AppView!
    appView.titleLabel?.text = apps.data[index].title
    appView.descLabel?.text = apps.data[index].desc
    appView.frame = CGRect(x:0, y:0, width:self.view.frame.width-30, height:carousel.frame.height-60)
    appView.backgroundColor = UIColor.white

    var shadowLayer: CAShapeLayer!
    shadowLayer = CAShapeLayer()
    shadowLayer.path = UIBezierPath(roundedRect: appView.bounds, cornerRadius: 0).cgPath
    shadowLayer.fillColor = UIColor.white.cgColor

    shadowLayer.shadowColor = UIColor.lightGray.cgColor
    shadowLayer.shadowPath = shadowLayer.path
    shadowLayer.shadowOffset = CGSize(width: 0.0, height: 0.0)
    shadowLayer.shadowOpacity = 0.8
    shadowLayer.shadowRadius = 2

    appView.layer.insertSublayer(shadowLayer, at: 0)

    appView.storeButton.addTarget(self, action: #selector(didTapApp), for: UIControlEvents.touchUpInside)

    DispatchQueue.main.async {
        appView.iconView?.sd_setImage(with: URL(string:self.apps.data[index].icon
            ), completed: { (image, error, cache, url) in
                if error == nil {
                    appView.iconView.image = appView.iconView.image?.cropToBounds(image: image!, width: 30, height: 30)
                }
        })
    }
    return appView
}


func carousel(_ carousel: iCarousel, valueFor option: iCarouselOption, withDefault value: CGFloat) -> CGFloat {
    if (option == .spacing) {
        return value * 1.1
    }
    return value
}


func didTapApp() {
    if self.apps.data.count != 0 {
        UIApplication.shared.open(URL(string: "itms://itunes.apple.com/app/id" + self.apps.data[carousel.currentItemIndex].link)!, options: [:], completionHandler: nil)
    }
}

func initApps() {
    xmlHelper.getAnoutherApps { (apps) in
        if apps != nil{
            self.apps = apps!
            self.carousel.reloadData()
        }
    }
}


override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
    let backItem = UIBarButtonItem()
    backItem.title = constants.back
    navigationItem.backBarButtonItem = backItem
    if segue.identifier == "toWeb" {
        let vc = segue.destination as! WebController
        vc.fileUrl = nil
        vc.url = self.selectUrl
        vc.title = self.selectTitle
        self.tabBarController?.tabBar.isHidden = true
    }
}

AppView(延迟消失)

class AppView : UIView {
@IBOutlet var titleLabel : UILabel!
@IBOutlet var descLabel : UILabel!
@IBOutlet var iconView : UIImageView!
@IBOutlet var storeButton : UIButton!
}

第一个控制器

class SettingsController : UITableViewController, MFMailComposeViewControllerDelegate {


var selectUrl : URL?
var selectTitle = ""
var selectStringUrl = ""

override func viewDidLoad() {
    super.viewDidLoad()
    self.initUI()
}

override func numberOfSections(in tableView: UITableView) -> Int {
    return 4
}

override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
    switch section {
    case 0:
        return 2
    case 1:
        return 1
    case 2:
        return 1
    case 3:
        return 3
    default:
        return 0
    }
}

override func tableView(_ tableView: UITableView, titleForHeaderInSection section: Int) -> String? {
    switch section {
    case 0:
        return constants.info
    case 1:
        return constants.settings
    case 2:
        return constants.connect
    case 3:
        return constants.community
    default:
        return ""
    }
}

override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
    let cell = tableView.dequeueReusableCell(withIdentifier: "settingsTitleCell")!
    let itemSize = CGSize(width:30, height:30);
    UIGraphicsBeginImageContextWithOptions(itemSize, false, UIScreen.main.scale);
    let imageRect = CGRect(x:0.0, y:0.0, width:itemSize.width, height:itemSize.height);
    cell.imageView?.image!.draw(in: imageRect)
    cell.imageView?.image! = UIGraphicsGetImageFromCurrentImageContext()!;
    UIGraphicsEndImageContext();
    switch indexPath.section {
    case 0:
        let cell = tableView.dequeueReusableCell(withIdentifier: "settingsTitleCell")!
        cell.textLabel?.text = constants.infoTitles[indexPath.row]
        cell.detailTextLabel?.text = constants.infoDetail[indexPath.row]
        cell.imageView?.image = cell.imageView?.image?.cropToBounds(image: constants.infoImages[indexPath.row], width: 30, height: 30)
        return cell
    case 1:
            let cell = tableView.dequeueReusableCell(withIdentifier: "fontCell") as! FontCell
            return cell
    case 2:
        let cell = tableView.dequeueReusableCell(withIdentifier: "settingsTitleCell")!
        cell.textLabel?.text = constants.connectTitles[indexPath.row]
        cell.imageView?.image = cell.imageView?.image?.cropToBounds(image: constants.connectImages[indexPath.row], width: 30, height: 30)
        cell.detailTextLabel?.text = ""
        return cell
    case 3:
        let cell = tableView.dequeueReusableCell(withIdentifier: "settingsTitleCell")!
        cell.textLabel?.text = constants.communityTitles[indexPath.row]
        cell.detailTextLabel?.text = constants.communityDetail[indexPath.row]
        cell.imageView?.image = cell.imageView?.image?.cropToBounds(image: constants.communityImages[indexPath.row], width: 30, height: 30)
        return cell
    default:
        return cell
    }
}

override func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
    if indexPath.section == 1 {
        return 80
    } else {
        return 45
    }
}

override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
    switch indexPath.section {
    case 0:
        if indexPath.row == 0 {
            self.initWebView(url: Bundle.main.url(forResource: "caution", withExtension: "html"), stringUrl: nil, titlePage: constants.infoTitles[indexPath.row])
        } else {
            self.selectTitle = constants.infoTitles[indexPath.row]
            self.performSegue(withIdentifier: "toApps", sender: self)
        }
    case 2:
        self.sendMessage()
        break;
    case 3:
        switch indexPath.row {
        case 0:
            self.initWebView(url: Bundle.main.url(forResource: "about", withExtension: "html"), stringUrl: nil, titlePage: constants.communityTitles[indexPath.row])
            break;
        case 1:
            self.initWebView(url: nil, stringUrl: "https://vk.com/electronicengineer", titlePage: constants.communityTitles[indexPath.row])
        case 2:
            self.initWebView(url: nil, stringUrl: "https://fb.com", titlePage: constants.communityTitles[indexPath.row])
            break;
        default:
            break;
        }
    default:
        break;
    }
}

func initUI() {
    self.title = constants.settings
    self.tableView.tableFooterView = UIView()
}

func initWebView(url:URL?, stringUrl:String?, titlePage:String) {
    if stringUrl != nil {
        self.selectStringUrl = stringUrl!
        self.selectUrl = nil
    } else {
        self.selectUrl = url!
    }
    self.selectTitle = titlePage
    self.performSegue(withIdentifier: "toWeb", sender: self)
}

func sendMessage() {
    let mailComposeViewController = configuredMailComposeViewController()
    if MFMailComposeViewController.canSendMail() {
        self.present(mailComposeViewController, animated: true, completion: nil)
    }
}

func configuredMailComposeViewController() -> MFMailComposeViewController {
    let mailComposerVC = MFMailComposeViewController()
    mailComposerVC.mailComposeDelegate = self
    mailComposerVC.setToRecipients(["postboxapp@yandex.ru"])
    mailComposerVC.setSubject("Электроник на Android")
    mailComposerVC.setMessageBody("", isHTML: false)
    return mailComposerVC
}


func mailComposeController(_ controller: MFMailComposeViewController, didFinishWith result: MFMailComposeResult, error: Error?) {
    controller.dismiss(animated: true, completion: nil)
}

override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
    let backItem = UIBarButtonItem()
    backItem.title = constants.back
    navigationItem.backBarButtonItem = backItem
    if segue.identifier == "toWeb" {
        let vc = segue.destination as! WebController
        vc.fileUrl = self.selectUrl
        vc.url = self.selectStringUrl
        vc.title = self.selectTitle
        self.tabBarController?.tabBar.isHidden = true
    }
    if segue.identifier == "toApps" {
        let vc = segue.destination as! AppsController
        vc.title = self.selectTitle
        self.tabBarController?.tabBar.isHidden = true
    }
}
}

2 个答案:

答案 0 :(得分:1)

问题解决了:

override func viewWillDisappear(_ animated: Bool) {
    super.viewWillDisappear(true)
    self.carousel.isHidden = true
}

override func viewWillAppear(_ animated: Bool) {
    super.viewWillAppear(true)
    self.carousel.isHidden = false
}

答案 1 :(得分:0)

隐藏和取消隐藏只是解决实际问题的一种方法。

answered to a similar question设置为yourCarousel.clipsToBounds = true