佚名通过本文主要向大家介绍了uiview,uiviewcontroller,uiview动画,ios uiview,uiview 圆角等相关知识,希望对您有所帮助,也希望大家支持linkedu.com www.linkedu.com
问题: 调用自定义uiview的方法
描述:
描述:
本帖最后由 wsxcy66668888 于 2015-04-19 19:27:00 编辑
uiview
一个网上找的代码:
import UIKit
let kTouchedLinkNotification = "kTouchedLinkNotification"
let kCWInvalidRange = NSMakeRange(NSNotFound, 0)
class CWCoreTextView: UIView, NSLayoutManagerDelegate {
var layoutManager: CWLayoutManager
var textContainer: NSTextContainer
var touchesGestureRecognizer: CWTouchesGestureRecognizer?
var touchRange = kCWInvalidRange
var textStorage: NSTextStorage? {
didSet {
if let _textStorage: NSTextStorage = textStorage {
_textStorage.addLayoutManager(layoutManager)
self.setNeedsUpdateConstraints()
self.setNeedsDisplay()
}
}
}
required init(coder aDecoder: NSCoder) {
layoutManager = CWLayoutManager()
textContainer = NSTextContainer(size: CGSizeMake(320, CGFloat.max))
super.init(coder: aDecoder)
layoutManager.delegate = self
layoutManager.addTextContainer(textContainer)
touchesGestureRecognizer = CWTouchesGestureRecognizer(target: self, action: "handleTouch:")
self.addGestureRecognizer(touchesGestureRecognizer!)
}
override func drawRect(rect: CGRect) {
if let _textStorage: NSTextStorage = textStorage {
let glyphRange = layoutManager.glyphRangeForTextContainer(textContainer)
let point = layoutManager.locationForGlyphAtIndex(glyphRange.location)
layoutManager.drawGlyphsForGlyphRange(glyphRange, atPoint: point)
}
}
func handleTouch(gestureRecognizer: UIGestureRecognizer) {
let state = gestureRecognizer.state
switch state {
// began
case .Began :
var location = gestureRecognizer.locationInView(self)
let startPoint = layoutManager.locationForGlyphAtIndex(0)
location = CGPoint(x: location.x - startPoint.x, y: location.y - startPoint.y)
var fraction: CGFloat = 0
let index = layoutManager.glyphIndexForPoint(location, inTextContainer: textContainer, fractionOfDistanceThroughGlyph: &fraction)
if (0.01 < fraction && fraction < 0.99) {
var effectiveRange: NSRange = kCWInvalidRange
var value: AnyObject? = textStorage?.attribute(NSLinkAttributeName, atIndex: index, effectiveRange: &effectiveRange)
if let _value: AnyObject = value {
touchRange = effectiveRange
layoutManager.touchRange = touchRange
layoutManager.isTouched = true
NSNotificationCenter.defaultCenter().postNotificationName(kTouchedLinkNotification, object: _value)
self.setNeedsDisplay()
} else {
touchRange = kCWInvalidRange
}
}
// end or canceled
case .Ended, .Cancelled :
if (touchRange.location != NSNotFound) {
&