Self Scrollable Text Inside Cascrolllayer

Self Scrollable text inside CAScrollLayer

One hardly needs or wants a CAScrollLayer or a CATextLayer for this. Simply start with a superview that clips to its bounds, and inside that put a UILabel. Animate the sideways movement of the label and you're done.

Here's a rough sketch:

    let w1 = self.lab.frame.width
let w2 = self.lab.superview!.frame.width
let w = w1 - w2
let x = self.lab.frame.origin.x
UIView.animateWithDuration(5, delay: 0, options: .CurveLinear, animations: {
self.lab.frame.origin.x -= w
}, completion: {
_ in
self.lab.frame.origin.x = x
})

Sample Image

CATextLayer not scrolling

How are you using contentsRect? It's a unit rectangle coordinates, that is each argument is between zero and one (inclusive).

So if you want to display 50 x 50 sub-rectangle at point 30, 30 you would do

textLayer.contentsRect = CGRectMake(30.0f / CONTENT_WIDTH, 30.0f / CONTENT_HEIGHT, 267.0f / CONTENT_WIDTH, 320.0f / CONTENT_HEIGHT);

Where CONTENT_WIDTH and CONTENT_HEIGHT are the size of the contents.

Mix CATextLayer and CAScrollLayer

CALayer (and CATextLayer) can be scrolled

How to use MarqueeLabel in Swift?

I used this little piece of code for my label to scroll like marquee:

@IBOutlet weak var firstLabel: UILabel! 
override func viewDidLoad() {
super.viewDidLoad()

UIView.animateWithDuration(12.0, delay: 1, options: ([.CurveLinear, .Repeat]), animations: {() -> Void in
self.firstLabel.center = CGPointMake(0 - self.firstLabel.bounds.size.width / 2, self.firstLabel.center.y)
}, completion: { _ in })
}

Yes, it worked.

Scroll NSTableView to endOfDocument not the last row

Your scrollRowToVisible approach should work. Here's a quick sample project that implements variable heights with a button that scrolls to the last row. That last row is fully visible after scrolling.

TableScroll image

Update:
For table cells larger than the surrounding NSClipView, the above technique will only scroll to the top of the cell. To scroll to the bottom of the last cell, you can use:

let point = NSPoint(x: 0, y: tableView.frame.height)
tableView.scroll(point)

or since OP was in ObjC:

[[self tableView] scrollPoint: NSMakePoint(0, [self tableView].frame.size.height)]


Related Topics



Leave a reply



Submit