Use drawRect to implement a ruler.

Source: Internet
Author: User

Use drawRect to implement a ruler.

The drawRect method is used to implement a ruler selector. The demo is here: https://github.com/phelthas/lxmrulervieweffect. If the reuse problem is not considered, I feel that the simplest implementation method is to make the art an image of a ruler, it can be placed on scrollView. The value can be calculated based on the contentOffset of scrollview. If the reuse problem is considered, it is still convenient to write code and encapsulate some attributes, You can implement many different styles. So we have this LXMRulerView ~ Implementation Scheme: currently, we are using a relatively stupid implementation scheme, which is to draw a Ruler Using drawRect and then place the view on scrollview, then, the contentOffset of scrollview is used to calculate the value. I have seen that the drawRect method will occupy more memory, and the CALayer implementation will be much better. I haven't tried it yet, but at present, the memory usage in the demo is very small, if you have time, try the implementation of layer ~ Why is this solution stupid? In fact, this is a complete ruler that does not reuse any resources. In theory, if the ruler is very long, it will consume a lot of memory; considering that the ruler is constantly repeating the appearance, only numbers are different, so theoretically it would be better to use a horizontal collectionView. If you have time to practice it, let's take a look at the effect ~ Note: 1. Support for autoLayout and support for drawRect initialized from xib are drawn by bounds. By default, the drawRect method is called only once. Therefore, if the size of the initialization view is different from the size of the final display view, drawRect is drawn based on the initial size and will not be adjusted with autoLayout, therefore, there is no good way to adjust the size of the view manually in layoutSubview. However, it is not enough to adjust the view Size. By default, the painted image will not be re-drawn. The UIView has a contentMode attribute and needs to be set to UIViewContentModeRedraw. The effect is that when the view bounds changes, the drawRect method is called again. 2. the integer adsorption effect is too low to adjust contentOffset in the scrollviewDidScroll method so that it stays at the integer position !!! Why does it make the animation effect look incoherent? Is used -(Void) ScrollViewWillEndDragging :(UIScrollView *) ScrollView withVelocity :(CGPoint) Velocity targetContentOffset :(InoutCGPoint*) TargetContentOffsetThis method is also the delegate method provided by the system, so you can rest assured that the effect is that scrollview can stay in TargetContentOffsetSo you only need to calculate the specified position in this method ~ For detailed usage, refer to my previous blog (the delegate method of UIScrollView makes UICollectionView slide to a certain position you want). 3. Callback for value change is easy to use, the value change callback method is block. When using it, you need to pay attention to the circular reference problem, so weakSelf is still used in the block. 4. To ensure the accuracy of the sliding position, the maximum and minimum values and spacing of the ruler only support integer types, floating point numbers are not supported ~~ Wait until you think of a solution to support it ~ There are still ongoing updates. If you have any questions, please discuss them ~

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.