ios - 如何在 Swift 中创建像指南针一样的 'sliding view' ?

标签 ios swift ios8 programmatically-created

在当前的指南针应用中,您可以在指南针和水平仪之间滑动。我如何在 Swift 中创建相同的效果?嗯,我正在尝试创建一个主要用于学习的应用程序。我想创建一个记事本应用程序,如果您向右滑动,则会出现字体选择,如果您向左滑动,则会出现以前的笔记列表。到目前为止,这是我的代码

    var noteArray: Dictionary<String,String> = [:]
    var selectTextView = UIButton()
    var selectTextField = UITextField()
    var selectString: String = ""
    override func viewDidLoad() {
    super.viewDidLoad()
    self.view.backgroundColor = UIColor.whiteColor()
    var widthField = self.view.bounds.size.width - 10
    var heightField = self.view.bounds.size.height - 69 - 221 - 38
    println(heightField)
    println(widthField)
    var textFieldString: String! = ""
    //Set up resizable Button Image
    let buttonImage: UIImage = UIImage(named:"whiteButtonLong")
    let buttonImageHighlight: UIImage = UIImage(named:"whiteButtonHighlightLong")
    //Set up text field
    self.textView = UITextView(frame: CGRectMake(5, 64, widthField, heightField))
    self.textView.backgroundColor = UIColor.redColor()
    self.view.addSubview(self.textView)
    textView.becomeFirstResponder()
    //Set up the New button
    var newButtonString: String! = "New Note"
    var heightButton = 568 - heightField - 1000
    var widthButton = (widthField/2) - 2
    let floatInt: CInt = 3
    let newButton = UIButton(frame: CGRect(x: 5, y: 5, width: widthButton, height: 50))
    UIButton.buttonWithType(UIButtonType.System)
    newButton.setTitle(newButtonString,forState: UIControlState.Normal)
    newButton.setTitleColor(UIColor.blackColor(), forState: UIControlState.Normal)
    newButton.setBackgroundImage(buttonImage, forState: UIControlState.Normal)
    newButton.setBackgroundImage(buttonImageHighlight, forState: UIControlState.Highlighted)
    /*newButton.backgroundColor = UIColor.cyanColor()
    newButton.layer.borderColor = UIColor.blackColor().CGColor
    let widthForBorder: CGFloat = 0.5
    newButton.layer.borderWidth = widthForBorder*/
    newButton.backgroundRectForBounds(CGRectMake(5, 5, widthButton, heightButton))
    newButton.addTarget(self, action: "buttonAction:", forControlEvents: UIControlEvents.TouchUpInside)
    newButton.layer.cornerRadius = 10
    newButton.clipsToBounds = true
    self.view.addSubview(newButton)
    //Set up select note
    let widthSelectField = widthButton - 10
    selectTextView = UIButton(frame: (CGRectMake(162, 5, widthButton, 50))) as UIButton
    selectTextView.setBackgroundImage(buttonImage, forState: UIControlState.Normal)
    selectTextView.setBackgroundImage(buttonImageHighlight, forState: UIControlState.Highlighted)
    self.view.addSubview(selectTextView)
    selectTextField = UITextField(frame: CGRectMake(170, 22.5, widthSelectField, 15))
    selectTextField.keyboardType = UIKeyboardType.NumberPad
    //selectTextField.backgroundColor = UIColor.blueColor()
    selectTextView.addTarget(self, action: "textViewAction:", forControlEvents: UIControlEvents.TouchUpInside)
    self.view.addSubview(selectTextField)
    selectTextField.text = selectString
    selectTextField.canBecomeFirstResponder()
    selectTextField.textAlignment = NSTextAlignment.Center
    selectTextField.placeholder = "Select Note"
    //Set up return button
    var returnButton: UIButton = UIButton(frame: CGRectMake(120, 315, 80, 30))
    returnButton.backgroundColor = UIColor.grayColor()
    returnButton.layer.cornerRadius = 10
    returnButton.layer.masksToBounds = true
    returnButton.layer.borderWidth = 2
    returnButton.layer.borderColor = UIColor.blackColor().CGColor
    returnButton.setTitleColor(UIColor.blackColor(), forState: UIControlState.Normal)
    returnButton.setTitle("Return", forState: UIControlState.Normal)
    returnButton.addTarget(self, action: "returnButtonAction:", forControlEvents: UIControlEvents.TouchUpInside)
    self.view.addSubview(returnButton)

这在 viewDidLoad() 之外

func buttonAction(sender:UIButton!) {
        var noteArrayPlusOne = String(noteArray.count + 1)
        noteArray.updateValue(textView.text, forKey: noteArrayPlusOne)
        println(noteArray)
        textView.text = ""
    }
    func textViewAction(sender: UIButton!) {
        selectTextField.becomeFirstResponder()
    }
    func returnButtonAction(sender: UIButton!){
        //println("button was pressed")
        selectString = selectTextField.text
        println(selectString)
        textView.text = noteArray[selectString]
        selectTextField.text = ""
    }

最佳答案

您想使用UIScrollView 。这为您提供了一个可以平移的 View 。

let scrollView: UIScrollView!

override func viewDidLoad() {
    super.viewDidLoad()
    self.scrollView = UIScrollView(frame: self.view.bounds)
    self.view.addSubview(self.scrollView)
}

要设置大小,请设置 UIScrollView 的 contentSize 属性。对于指南针/水平仪应用程序:

self.scrollView.contentSize = CGSize(width: 2 * theWidthOftheScreen, height: theHeightOfTheScreen)

要使 ScrollView “捕捉”到最左边或最右边的滚动位置,请启用分页:

self.scrollView.pagingEnabled = true

还有很多其他属性,例如更改滚动条颜色或隐藏它们。

关于ios - 如何在 Swift 中创建像指南针一样的 'sliding view' ?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24224214/

相关文章:

iphone - iOS 4 通常向后兼容吗?

ios - 连接到 Apple Music

swift - 当 bool 值为 true 时如何删除该节点?

swift - 使用 UIPanGestureRecognizer 的输入实现自然滚动算法

ios - 如何在 Today 扩展小部件上使用 UIAutomation?

ios - 自定义 UILabel View 未更新其在 Interface Builder 中的框架

ios - 使应用程序出现在共享面板中以接受来自其他应用程序的选定文本

xcode - 使用未解析的标识符 'self'

即日起 iOS 8 应用程序提交和新版本应用程序

ios - AutoLayout 限制 View 位于键盘正上方