Custom pageControl with elastic effect and elastic pagecontrol

Source: Internet
Author: User

Custom pageControl with elastic effect and elastic pagecontrol

It is implemented in three parts. In the drawrect method, the gray background is drawn, and the dotView of the corresponding number is created based on pageCount, which is placed in the corresponding position and hidden. A CAShapeView layer is created, construct the besell Curve Based on the offset of scrollView, draw a red line, and deform the big circle.

Big circle deformation and construction ideas:

(Figure referenced from:

The following points are constructed for A, B, C, D, c1, c2, c3, c4, c5, c6, c7, and c8, give A, B, C, D, four points, A shape variable about contentOffset to achieve the effect of garden deformation. Value = (radius * Ratio) * deformation factor, in which the deformation factor is related to contentOffset (this relationship is formed when a page is sliding: 0 ~ 0.5 ~ 0); CGFloat factor = MIN (0.5, MAX (0, (ABS (scrollView. contentOffset. x-last)/scrollView. frame. size. width), after creating a better point, update the coordinate of the Point Based on the offset to generate a new besell curve, and then generate a deformed circle.

The following is some code:

/** Update the beiser curve */-(void) updateDotLayerScrollView * Based on scrollView: (UIScrollView *) scrollView {// determine the sliding direction BOOL left = (scrollView. contentOffset. x-self. lastOffsetX)> = 0? YES: NO; // sliding to the left will show dotView if (left) {[self setDotViewShowWithScrollView: scrollView];} CGPoint dotCenter = [self calculateDotCenterWithScrollView: scrollView]; // calculate the cursor location of the current offset (more than half belongs to the latter, and vice versa) int count = (int) (scrollView. contentOffset. x/scrollView. frame. size. width + 0.5); // calculates the offset CGFloat last = scrollView of the cursor location based on count. frame. size. width * count; CGFloat factor = MIN (0.5, MAX (0, (ABS (scrollView. cont EntOffset. x-last)/scrollView. frame. size. width); // extra is the displacement of four points A, B, C, and D, which is related to factor. The relationship between factor and contentOffset is 0 ~ 0.5 ~ 0; CGFloat extra = self. seletedDotRadiu * 4/5 * factor; // calculate the correlation points of the besell curve. A = CGPointMake (dotCenter. x, dotCenter. y-self. seletedDotRadiu + extra); B = CGPointMake (left? (DotCenter. x + self. seletedDotRadiu) :( dotCenter. x + self. seletedDotRadiu + extra * 2), dotCenter. y); C = CGPointMake (dotCenter. x, dotCenter. y + self. seletedDotRadiu-extra); D = CGPointMake (left? (DotCenter. x-self.SeletedDotRadiu-extra * 2) :( dotCenter. x-self.SeletedDotRadiu), dotCenter. y); // The 1.8 value should be calculated, but I don't know how to calculate it. I tried CGFloat offset = self. seletedDotRadiu/1.8; c1 = CGPointMake (. x + offset,. y); c2 = CGPointMake (B. x, B. y-offset); c3 = CGPointMake (B. x, B. y + offset); c4 = CGPointMake (C. x + offset, C. y); c5 = CGPointMake (C. x-offset, C. y); c6 = CGPointMake (D. x, D. y + offset); c7 = CGPointMake (D. x, D. y-offset); c8 = CGPointMake (. x-offset,. y); UIBezierPath * ovalPath = [UIBezierPath bezierPath]; CGPoint startPoint = CGPointMake (self. seletedDotRadiu, CGRectGetHeight (self. frame)/2.0); [ovalPath moveToPoint: startPoint]; [ovalPath addLineToPoint: D]; [ovalPath addCurveToPoint: C controlPoint1: c6 controlPoint2: c5]; [ovalPath addCurveToPoint: B controlPoint1: c4 controlPoint2: c3]; [ovalPath addCurveToPoint: A controlPoint1: c2 controlPoint2: c1]; [ovalPath addCurveToPoint: D controlPoint1: c8 controlPoint2: c7]; [ovalPath closePath]; self. dotLayer. path = ovalPath. CGPath; self. lastOffsetX = scrollView. contentOffset. x ;}

DrawRect method:

/** Draw the initial background */-(void) drawRect :( CGRect) rect {// control vertical center CGFloat verticalCenter = rect. size. height/2.0; // the radius of the dot CGFloat dotR = self. dotRadiu; // select the dot radius CGFloat selectedDotR = self. seletedDotRadiu; // The Center is located at CGFloat distanceCenter = (rect. size. width-2 * selectedDotR)/(self. pageCount-1); UIBezierPath * path = [UIBezierPath bezierPathWithRect: CGRectMake (selectedDotR, (rect. size. height-self. lineWidth)/2.0, rect. size. width-2 * selectedDotR, self. lineWidth)]; [self. color setFill]; for (int I = 0; I <self. pageCount; I ++) {UIBezierPath * dotPath = [UIBezierPath paths: CGPointMake (selectedDotR + distanceCenter * I, verticalCenter) radius: dotR startAngle: 0 endAngle: M_PI * 2 clockwise: YES]; [path appendPath: dotPath];} [path fill];}


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: 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.