Join the social network of Tech Nerds, increase skill rank, get work, manage projects...
 
  • How to create a custom Spinner

    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 279
    Comment on it

    To create a custom spinner create a custom view with the following initialization method

    - (id)initWithFrame:(CGRect)frame superViewFrame:(CGRect)superViewFrame
    {
        self = [super initWithFrame:frame];
        if (self) {
            // Initialization code
    
            self.layer.cornerRadius = frame.size.height/2;
            self.backgroundColor = [UIColor grayColor];
            self.layer.borderColor = [UIColor grayColor].CGColor;
    
    
            height = superViewFrame.size.height/2;
            center = CGPointMake(height, height);
    
            count = 0;
            x = 0;
        }
        return self;
    }
    

    Now create a method which will rotate custom view in circular path

    -(void)fameForView{
        count += 3;
    
        if (count >= 180) {
    
            count = 0;
        }
        x = sinf(DEGREES_TO_RADIANS(count))*height*2;
    
        if (x < 0) {
            x = 0;
        }
    
    
        CGFloat pwr;
    
        pwr = sqrt((powf(height,2) - powf((x - center.x), 2)));
    
        BOOL isRotating = YES;
        if (count >= 90) {
            isRotating = NO;
        }
    
    
        if (isRotating) {
    
            y = center.y - pwr;
        }else{
    
            y = center.y + pwr;
        }
    
    
        CGFloat decreaseSize = 1.0/(8.0);
        CGRect frame = self.frame;
        if (x != 0) {
            frame.size.width -= decreaseSize;
            frame.size.height -= decreaseSize;
    
        }else{
    
            frame.size.width = 15;
            frame.size.height = 15;
        }
        [self setFrame:frame];
        self.center = CGPointMake(x, y);  
    }
    

    add animation while rotating the custom view with following method

    -(void)animateView{
        [UIView animateWithDuration:0.02 animations:^{
    
                [self fameForView];
    
    
        } completion:^(BOOL finished) {
            [self animateView];
        }];
    }
    

    Happy coding......

 0 Comment(s)

Sign In
                           OR                           
                           OR                           
Register

Sign up using

                           OR                           
Forgot Password
Fill out the form below and instructions to reset your password will be emailed to you:
Reset Password
Fill out the form below and reset your password: