开发者

Move left and right in UIScrollView

I have a UIScrollView which scrolls only horizontally. I have a situation where I have views filled by array in UIScrollView. There are three views which altogether make one single component in UIScrollView (there are many such components). Component contains :

  • UILabel
  • UIButton
  • UIView (for underlying text)

I have placed two arrow keys(left and right) at the ends of UIScrollView. On touching left arrow I am shifting one position left (with complete string to display) in UIScrollView and one position right (with complete string to display) in case of right arrow button.

Till now I am able to perform left arrow action. But I have no idea how to perform right arrow touch and how to display complete string to touching arrow keys if only part of that string is being displayed.

I know it is weird that I have a UIScrollView and still want to add arrow keys to move it. But can't help it. This is client's requirement. The following is how I implemented left arrow action:

-(IBAction) onClickLeftArrow
{
    NSLog(@"BUTTON VALUE : %i",iButtonValue);
    if(iButto开发者_Go百科nValue <= [m_BCListArray count]) {
        NSArray *arr = [scrollDemo subviews];
        for(int j = 0; j < [arr count]; j++) {
            UIView *view1 = [arr objectAtIndex:j];
            [view1 removeFromSuperview];
        }
        XX = 5.0;
        int tagcount = [m_BCListArray count]-iButtonValue;
        NSLog(@"%@",m_BCListArray);
        for(int i = iButtonValue; i >= 1; i--)
        {
            UILabel *blabel = [[UILabel alloc] initWithFrame:CGRectMake(XX, 6, 120, 26)];
            blabel.text = [m_BCListArray objectAtIndex:[m_BCListArray count]-i];
            blabel.backgroundColor = [UIColor clearColor];
            blabel.textColor = [UIColor whiteColor];
            blabel.font = [UIFont systemFontOfSize:14];
            [scrollDemo addSubview:blabel];

            //underline code
            CGSize expectedLabelSize = [[m_BCListArray objectAtIndex:[m_BCListArray count]-1] sizeWithFont:blabel.font constrainedToSize:blabel.frame.size lineBreakMode:UILineBreakModeWordWrap];
            CGRect newFrame = blabel.frame;
            newFrame.size.height = expectedLabelSize.height;
            blabel.frame = CGRectMake(blabel.frame.origin.x, blabel.frame.origin.y, expectedLabelSize.width+10, expectedLabelSize.height);
            blabel.numberOfLines = 1;
            [blabel sizeToFit];
            int width=blabel.bounds.size.width;
            int height=blabel.bounds.size.height;

            UIButton *btnContent = [UIButton buttonWithType:UIButtonTypeCustom];
            [btnContent addTarget:self action:@selector(SelectButton:)forControlEvents:UIControlEventTouchDown];
            btnContent.tag = tagcount+1;
            btnContent.frame=CGRectMake(XX, 2.0, width+10, height);
            [scrollDemo addSubview:btnContent];
            scrollDemo.contentSize = CGSizeMake(XX+50, 32);
            //scrollDemo.contentSize = CGSizeMake(expectedLabelSize.width,expectedLabelSize.height);

            UIView *viewUnderline=[[UIView alloc] init];
            viewUnderline.frame=CGRectMake(XX, 26, width, 1);
            viewUnderline.backgroundColor=[UIColor whiteColor];
            [scrollDemo addSubview:viewUnderline];
            [viewUnderline release];

            scrollDemo.contentSize = CGSizeMake(XX+width+10, 32);
            XX = XX + width+10;
            iRight  = iRight + 1;
            tagcount ++;
          //  iRight --;
        }
    }   
    iButtonValue = iButtonValue+1;
}


Set the contentOffset of UIScrollView on the Button click action and u'll get your desired effect.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜