美文网首页
Lecture6 Multitouch

Lecture6 Multitouch

作者: 快乐捣蛋鬼 | 来源:发表于2019-07-20 11:10 被阅读0次
lecture6_Today.png
lecture6_Demo

Notes

1.Redraw

画一个圆之后,如果将手机从正常换为landscape,圆会变成椭圆,需要将view的content mode 改为Redraw,这样在改变bound的时候,就会重绘。

override func draw(_ rect: CGRect) {
    
    // Solution 1
    if let context = UIGraphicsGetCurrentContext() {
        context.addArc(center: CGPoint(x: bounds.midX, y: bounds.midY), radius: 100.0, startAngle: 0, endAngle: 2*CGFloat.pi, clockwise: true)
        context.setLineWidth(5.0)
        UIColor.green.setFill()
        UIColor.red.setStroke()
        context.strokePath()
        context.fillPath()
    }
    
    // Solution 2
    let path = UIBezierPath()
    path.addArc(withCenter: CGPoint(x: bounds.midX, y: bounds.midY), radius: 100.0, startAngle: 0, endAngle: 2*CGFloat.pi, clockwise: true)
    path.lineWidth = 5.0
    UIColor.green.setFill()
    UIColor.red.setStroke()
    path.stroke()
    path.fill()
    
}
normal.png
landscape.png
redraw.png

2.居中且随着系统字体大小改变的字体

fontSize
private func centeredAttributedString(_ string: String, fontsize: CGFloat) -> NSAttributedString {
    
    var font = UIFont.preferredFont(forTextStyle: .body).withSize(fontsize)
    font = UIFontMetrics(forTextStyle: .body).scaledFont(for: font)
    
    let paragraphStyle = NSMutableParagraphStyle()
    paragraphStyle.alignment = .center
    
    return NSAttributedString(string: string, attributes: [NSAttributedString.Key.paragraphStyle: paragraphStyle, NSAttributedString.Key.font: font])
    
}

3. layoutSubviews()

用于subview的布局。当subview的bounds改变时,会由系统调用layoutSubviews()里的代码。
layoutSubviews()由系统调用,如果你想要调用它,调用setNeedsLayout(),然后系统最终会调用layoutSubviews()
如果你调用setNeedsDisplay(),系统最终会调用draw()

4.label.sizeToFit()

它会调整标签的大小以适应其内容。但是如果该lable已经有了宽度,调用sizeToFit(),会使label变的更高,所以要先调用label.frame.size = CGSize.zero。在做sizeToFit之前先清除它的大小。

lable.frame.size = CGSize.zero
lable.sizeToFit()

5.traitCollectionDidchange()

当调整字体大小的滑块变化时,会调用这个方法,当系统字体大小改变时,需要在这个方法里调用setNeedsDisplay()setNeesdLayout()从而改变view里字体的大小,以及重新调整布局。

override func traitCollectionDidChange(_ previousTraitCollection: UITraitCollection?) {
    setNeedsDisplay()
    setNeedsLayout()
}

6.@IBDesignable 和 @IBInspectable

在class前面加上@IBDesignable可以直接在InterfaceBuiler中编译显示。

import UIKit
@IBDesignable
class PlayingCardView: UIView {

}

7.为UIView添加一个 gesture recognizer

Image we wanted a UIView in our Controller's view to recognize a "pan" gesture.
we can configure it to do so in the property observer for the outlet to that UIView...

    @IBOutlet weak var pannableView: UIView {
        didSet {
            let panGestureRecognizer = UIPanGestureRecognizer(target: self, action: #selector(panAction(recognizer:)))
            pannableView.addGestureRecognizer(panGestureRecognizer)
        }
    }
    
    @objc func panAction(recognizer: UIPanGestureRecognizer) {
        switch recognizer.state {
        case .changed: fallthrough
        case .ended:
            let translation = recognizer.translation(in: pannableView)
            // update
            recognizer.setTranslation(CGPoint.zero, in: pannableView)
        default:
            break
        }
        
    }

8.通过InterfaceBuilder添加tap手势

1.给view添加一个tapGesture


lecture6_addTapGesture

2.为tapGesture添加Action


lecture6_addActionToTap

@IBAction func FlipCard(_ sender: UIPanGestureRecognizer) {
    switch sender.state {
    case .ended:
        playingCardView.isFacedUp = !playingCardView.isFacedUp
    default:
        break
    }
}

相关文章

网友评论

      本文标题:Lecture6 Multitouch

      本文链接:https://www.haomeiwen.com/subject/psjslctx.html