UIButton's image scrolling with scrollview but its action/method remains on original position

前端 未结 1 1120
一生所求
一生所求 2021-01-26 09:25

I have a UIScrollView that scrolls automatically, and I create several buttons using initWithFrame, each with an image and an unique action(method) and add them all to the scrol

相关标签:
1条回答
  • 2021-01-26 10:03

    try this code..

    in your view did load method

     float x=62;
        for(int i=0;i<4;i++)
        {
            self.stack=[UIButton buttonWithType:UIButtonTypeCustom];
            self.stack.frame=CGRectMake(x+(62*i), 100, 52, 52);
            [self.stack setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
            [self.stack setTitle:[self.titleName objectAtIndex:i] forState:UIControlStateNormal];
            [self.stack setBackgroundColor:[UIColor redColor]];
            self.stack.tag=i;
            [self.stack addTarget:self action:@selector(actionMethod:) forControlEvents:UIControlEventTouchUpInside];
            //your scroll view
            //[self.scrView addSubview:self.btnlabe1];
            [self.view addSubview:self.stack];
        }
    

    and create method like this...

    -(void)actionMethod:(id)sender
    {
        UIButton *button=(UIButton *)sender;
        switch (button.tag) {
    
                case 0:
                NSLog(@"ACTIONMETHOD CODE HERE");
                break;
            case 1:
                NSLog(@"ACTIONMETHOD1 CODE HERE");
                break;
            case 2:
                NSLog(@"ACTIONMETHOD2 CODE HERE");
                break;
            case 3:
                NSLog(@"ACTIONMETHOD3 CODE HERE");
                break;
    
            default:
                break;
        }
    }
    

    edited code

    float x=62;
        for(int i=0;i<4;i++)
        {
            self.stack=[UIButton buttonWithType:UIButtonTypeCustom];
            self.stack.frame=CGRectMake(x+(62*i), 100, 52, 52);
            [self.stack setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
            [self.stack setTitle:[temp objectAtIndex:i] forState:UIControlStateNormal];
            [self.stack setBackgroundColor:[UIColor redColor]];
            switch (i) {
                case 0:
                    [self.stack addTarget:self action:@selector(aMethod0) forControlEvents:UIControlEventTouchUpInside];
                    break;
                case 1:
                    [self.stack addTarget:self action:@selector(aMethod1) forControlEvents:UIControlEventTouchUpInside];
                    break;
                case 2:
                    [self.stack addTarget:self action:@selector(aMethod2) forControlEvents:UIControlEventTouchUpInside];
                    break;
                case 3:
                    [self.stack addTarget:self action:@selector(aMethod3) forControlEvents:UIControlEventTouchUpInside];
                    break;
                default:
                    break;
            }        //your scroll view
            //[self.scrView addSubview:self.stack];
            [self.view addSubview:self.stack];
        }
    

    and methods

    -(void)aMethod0
    {
        NSLog(@"action 0");
    
    }
    -(void)aMethod1
    {
        NSLog(@"action 1");
    
    }
    -(void)aMethod2
    {
        NSLog(@"action 2");
    
    }
    -(void)aMethod3
    {
        NSLog(@"action 3");
    
    }
    
    0 讨论(0)
提交回复
热议问题