久久久久久久av_日韩在线中文_看一级毛片视频_日本精品二区_成人深夜福利视频_武道仙尊动漫在线观看

使用自動布局時如何使 UIScrollView 僅向一個方向縮

How to make UIScrollView zoom in only one direction when using auto layout(使用自動布局時如何使 UIScrollView 僅向一個方向縮放)
本文介紹了使用自動布局時如何使 UIScrollView 僅向一個方向縮放的處理方法,對大家解決問題具有一定的參考價值,需要的朋友們下面隨著小編來一起學習吧!

問題描述

我正在嘗試使 UIScrollView 只允許在水平方向上縮放.滾動視圖是使用純自動布局方法設置的.通常的方法是在許多 Stack Overflow 答案(例如

但是,當使用自動布局時,內容偏移在縮放過程中最終會出錯.雖然內容視圖僅在水平方向縮放,但它會垂直移動:

我在 GitHub 上放了一個示例項目 (用于制作此問題中的視頻).它包含兩個故事板,Main.storyboard 和 NoAutolayout.storyboard,除了 Main.storyboard 啟用了自動布局而 NoAutolayout 沒有之外,它們是相同的.通過更改主界面項目設置在它們之間切換,您可以看到兩種情況下的行為.

我真的不想關閉自動布局,因為它以比手動布局所需的更好的方式解決了許多實現 UI 的問題.有沒有辦法在使用自動布局進行縮放時保持垂直內容偏移正確(即零)?

我在示例項目中添加了第三個故事板 AutolayoutVariableColumns.storyboard.這會添加一個文本字段來更改 GridView 中的列數,從而更改其固有內容大小.這更接近地顯示了我在提示此問題的真實應用中需要的行為.

解決方案

我想我想通了.您需要在轉換中應用平移以抵消 UIScrollView 在縮放時嘗試做的居中.

將此添加到您的 GridView:

var unzoomedViewHeight: CGFloat?覆蓋 func layoutSubviews() {super.layoutSubviews()unzoomedViewHeight = frame.size.height}覆蓋 var 變換:CGAffineTransform {得到 { 返回超級轉換 }放 {如果讓 unzoomedViewHeight = unzoomedViewHeight {var t = 新值t.d = 1.0t.ty = (1.0 - t.a) * unzoomedViewHeight/2super.transform = t}}}

要計算變換,我們需要知道視圖的未縮放高度.在這里,我只是在 layoutSubviews() 期間獲取幀大小并假設它包含未縮放的高度.可能存在一些不正確的極端情況;在視圖更新周期中計算高度可能是一個更好的地方,但這是基本思想.

I'm attempting to make a UIScrollView only allow zooming in the horizontal direction. The scroll view is setup with a pure autolayout approach. The usual approach is as suggested in a number of Stack Overflow answers (e.g. this one) and other resources. I have successfully used this in apps before autolayout existed. The approach is as follows: in the scroll view's content view, I override setTransform(), and modify the passed in transform to only scale in the x direction:

override var transform: CGAffineTransform {
    get { return super.transform }
    set {
        var t = newValue
        t.d = 1.0
        super.transform = t
    }
}

I also reset the scroll view's content offset so that it doesn't scroll vertically during the zoom:

func scrollViewDidZoom(scrollView: UIScrollView) {
    scrollView.contentSize = CGSize(width: scrollView.contentSize.width, height: scrollView.frame.height)
    scrollView.contentOffset = CGPoint(x: scrollView.contentOffset.x, y: 0.0)
}

This works very nicely when not using autolayout:

However, when using autolayout, the content offset ends up wrong during zooming. While the content view only scales in the horizontal direction, it moves vertically:

I've put an example project on GitHub (used to make the videos in this question). It contains two storyboards, Main.storyboard, and NoAutolayout.storyboard, which are identical except that Main.storyboard has autolayout turned on while NoAutolayout does not. Switch between them by changing the Main Interface project setting and you can see behavior in both cases.

I'd really rather not switch off autolayout as it solves a number of problems with implementing my UI in a much nicer way than is required with manual layout. Is there a way to keep the vertical content offset correct (that is, zero) during zooming with autolayout?

EDIT: I've added a third storyboard, AutolayoutVariableColumns.storyboard, to the sample project. This adds a text field to change the number of columns in the GridView, which changes its intrinsic content size. This more closely shows the behavior I need in the real app that prompted this question.

解決方案

Think I figured it out. You need to apply a translation in the transform to offset the centering UIScrollView tries to do while zooming.

Add this to your GridView:

var unzoomedViewHeight: CGFloat?
override func layoutSubviews() {
    super.layoutSubviews()
    unzoomedViewHeight = frame.size.height
}

override var transform: CGAffineTransform {
    get { return super.transform }
    set {
        if let unzoomedViewHeight = unzoomedViewHeight {
            var t = newValue
            t.d = 1.0
            t.ty = (1.0 - t.a) * unzoomedViewHeight/2
            super.transform = t
        }
    }
}

To compute the transform, we need to know the unzoomed height of the view. Here, I'm just grabbing the frame size during layoutSubviews() and assuming it contains the unzoomed height. There are probably some edge cases where that's not correct; might be a better place in the view update cycle to calculate the height, but this is the basic idea.

這篇關于使用自動布局時如何使 UIScrollView 僅向一個方向縮放的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!

【網站聲明】本站部分內容來源于互聯網,旨在幫助大家更快的解決問題,如果有圖片或者內容侵犯了您的權益,請聯系我們刪除處理,感謝您的支持!

相關文檔推薦

how to set scrollview content size in swift 3.0(如何在 swift 3.0 中設置滾動視圖內容大小)
Stop a UITableView from automatically scrolling(阻止 UITableView 自動滾動)
iOS UIScrollView Lazy Loading(iOS UIScrollView 延遲加載)
using iOS 6.0 SDK and building for iOS 5 Target causes UIScrollView setMinimumZoomScale to fail when running on iOS 5 simulator(在 iOS 5 模擬器上運行時,使用 iOS 6.0 SDK 并為 iOS 5 Target 構建會導致 UIScrollView setMinimumZ
Create partial-screen UIPageViewController programmatically(以編程方式創建部分屏幕 UIPageViewController)
how to make an ImageView zoomable with or without ScrollView.?(如何使用或不使用 ScrollView 使 ImageView 可縮放?)
主站蜘蛛池模板: 亚洲啪啪 | 91大神新作在线观看 | 精品一区久久 | 亚洲播放 | 欧美一区二区三区国产 | 国产韩国精品一区二区三区 | 蜜桃一区二区三区在线 | 欧美性生活视频 | 国产9999精品| 蜜臀网 | 99精品99| 二区三区视频 | 亚洲最新网址 | 日韩在线中文字幕 | 亚洲精品久久久蜜桃网站 | 婷婷成人在线 | 日韩欧美亚洲 | 精品一区二区三区在线观看国产 | 国产精品美女久久久久aⅴ国产馆 | 亚洲iv一区二区三区 | 欧美一区免费 | 日韩欧美一区二区三区免费看 | 欧美白人做受xxxx视频 | 一区二区三区在线电影 | 狠狠干天天干 | 少妇一级淫片免费放播放 | 精品久久久久久久久久久久久久 | 欧美精品久久久久 | 狠狠ri| 亚洲a人| 国产传媒 | 欧美精品久久久久久久久久 | 久草青青草| 国产一区二区三区 | 亚洲在线视频 | 国产精品欧美一区二区三区不卡 | 国产精品久久久久久久久久久久久久 | 国产精品久久精品 | 最新中文字幕在线 | 亚洲国产精品久久久久秋霞不卡 | av永久免费 |