UIButton的图像使用scrollview滚动,但其动作/方法仍保留在原始位置 [英] UIButton's image scrolling with scrollview but its action/method remains on original position

查看:160
本文介绍了UIButton的图像使用scrollview滚动,但其动作/方法仍保留在原始位置的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个自动滚动的UIScrollView,我使用initWithFrame创建了几个按钮,每个按钮都有一个图像和一个独特的动作(方法),并将它们全部添加到滚动视图中。按钮的图像可以使用滚动视图自动滚动,但是当我单击每个按钮时,它不是我分配给该按钮的方法。当我点击相同的位置时,无论哪个按钮经过,它都会转到相同的方法而不是该按钮的方法。下面是我创建按钮的代码:

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 scroll view. The buttons' images can be scrolling automatically with the scroll view but when I click on each button, it's not the method I assigned to that button. When I click on the same position, no matter which buttons pass by, it goes to the same method instead of the method of that button. Below is my code for creating the buttons:

for (int i = 0; i < 4; i++) {
        //create an imageView object in every 'page' of the scrollView.
        CGRect frame;
        frame.origin.x = 62 * i;
        frame.origin.y = 0;
        frame.size = CGSizeMake(52.0, 52.0);
        myButton = [[UIButton alloc] initWithFrame:frame];
        [myButton setImage:[MyImageArray objectAtIndex:i] forState:normal];
            switch (i) {
                case 0:
                    [myButton addTarget:self action:@selector(aMethod0) forControlEvents:UIControlEventTouchUpInside];
                    break;
                case 1:
                    [myButton addTarget:self action:@selector(aMethod1) forControlEvents:UIControlEventTouchUpInside];
                    break;
                case 2:
                    [myButton addTarget:self action:@selector(aMethod2) forControlEvents:UIControlEventTouchUpInside];
                    break;
                case 3:
                    [myButton addTarget:self action:@selector(aMethod3) forControlEvents:UIControlEventTouchUpInside];
                    break;
                default:
                    break;
            }
        [self.MyScrollView addSubview:myButton];

以下是自动滚动滚动视图的代码:

Below is the code for the automatically scroll the scrollview:

- (void)move_pagination {

    UIPageControl *pgCtr = MyPageControl;

    CGFloat contentOffset = MyScrollView.contentOffset.x;
    __block int  nextPage = (int)(contentOffset/(MyScrollView.frame.size.width+10)) + 1 ;

    [UIView animateWithDuration:10
                          delay:0
                        options:UIViewAnimationOptionCurveLinear | UIViewAnimationOptionAllowUserInteraction
                     animations:^{
                         if( nextPage!=3 )  {
                             MyScrollView.contentOffset = CGPointMake(nextPage*(10+MyScrollView.frame.size.width), 0);
                             pgCtr.currentPage=nextPage;
                             // else start sliding from 1 :)
                         } else {
                             nextPage = 1;
                             MyScrollView.contentOffset = CGPointMake(0, 0);
                             MyScrollView.contentOffset = CGPointMake(nextPage*(10+MyScrollView.frame.size.width), 0);
                             pgCtr.currentPage=2;
                         }

                     }
                     completion:nil];
}

为什么会出现这种奇怪的行为?谢谢。

Any clues why this strange behaviour happens?? Thanks.

推荐答案

在您的视图中试用此代码..

try this 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:[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;
    }
}

已编辑的代码

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];
    }

和方法

-(void)aMethod0
{
    NSLog(@"action 0");

}
-(void)aMethod1
{
    NSLog(@"action 1");

}
-(void)aMethod2
{
    NSLog(@"action 2");

}
-(void)aMethod3
{
    NSLog(@"action 3");

}

这篇关于UIButton的图像使用scrollview滚动,但其动作/方法仍保留在原始位置的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆