触摸scrollView上的功能

时间:2012-12-14 10:42:46

标签: iphone objective-c ios ios5 uiscrollview

我正在开发一个应用程序,我的任务是从数据库中获取url-links,我创建了一个滚动视图并在滚动条上放置了图像。
我写了一些代码,当我点击图像时,我必须在webview中显示url页面,我将其放在滚动视图上方。
为此我写了触摸代码,当我点击图像时获取网址,但我遇到的问题,如我的点击不工作的图像和scrollview也,但它正在图像和scrollview的下部工作是一个普通的观点。你可以在下面找到我的触摸代码

- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{   
    UITouch * touch = [[event allTouches] anyObject];   
    for(int index=0;index<[array count];index++)
{

    UIImageView *imgView = [array objectAtIndex:index];  
      if(CGRectContainsPoint([imgView frame], [touch locationInView:scrollView]))
        {  
            NSString *strgetcontenturl = [[NSString alloc]initWithString:[arrayvideoFile objectAtIndex:index]];  
            NSURL *url = [NSURL URLWithString:strgetcontenturl];  
            [webview loadRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:strgetcontenturl]]];
            NSURLRequest *requestObj = [NSURLRequest requestWithURL:url];  
             [webview loadRequest:requestObj];

        }}}

这里数组是我存储所有图像init的地方, arrayvideoFile 是我存储所有网址的地方。

请帮忙。

3 个答案:

答案 0 :(得分:0)

您是如何将图片放入scollview的? ImageView的?

我会使用UIButton(CustomType)放置它们,然后您可以将目标添加到该按钮,而无需检测触摸。因为UIButton已经检测到触摸事件。

UIButton *button = [UIButton butttonWithType:UIButtonTypeCustom];
UIImage * buttonImage = ....
[button setImage:buttonImage forState:UIControlStateNormal];
button.frame = CGRectMake(x, y, buttonImage.size.width, buttonImage.size.height);
[button addTarget:self
           action:@selector(buttonAction:)
 forControlEvents:UIControlEventTouchUpInside];
[scrollview addSubview:button];

编辑:添加了代码

答案 1 :(得分:0)

只需设置imgView.userInteractionEnabled=YES;它就可以帮助您。

否则使用此代码

UIScrollView *scrollview4preview=[[UIScrollView alloc]initWithFrame:CGRectMake(0, 44, self.view.frame.size.width, self.view.frame.size.height-44)];
    scrollview4preview.backgroundColor=[UIColor clearColor];
    [self.view addSubview:scrollview4preview];

    int yval=0;
    for (int i=0; i<[arrary count]; i++) {
        UIButton *btn4preview=[[UIButton alloc]init];
        btn4preview.tag=i;
        btn4preview.frame=CGRectMake(scrollview4preview.frame.origin.x+10, i*yval, 150, 150);
        [btn4preview setImage:[UIImage imageNamed:@"YourImageName.png"] forState:UIControlStateNormal];
        [btn4preview addTarget:self action:@selector(btnPreviewSelect:) forControlEvents:UIControlEventTouchUpInside];
        btn4preview.autoresizingMask=UIViewAutoresizingFlexibleLeftMargin;
        [scrollview4preview addSubview:btn4preview];

    }

    scrollview4preview.contentSize=CGSizeMake(scrollview4preview.frame.size.width, yval+160);


-(void)btnPreviewSelect:(id)sender
{
    UIButton *btn=(UIButton *)sender;

NSString *strgetcontenturl = [[NSString alloc]initWithString:[arrary objectAtIndex:btn.tag]];
NSURL *url = [NSURL URLWithString:strgetcontenturl];
[webview loadRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:strgetcontenturl]]];
NSURLRequest *requestObj = [NSURLRequest requestWithURL:url];
[webview loadRequest:requestObj];

}

答案 2 :(得分:0)

使用以下代码。

for(int Tag =0; Tag < [array count]; Tag++) {
    UIButton *btn_URL = [UIButton butttonWithType:UIButtonTypeCustom];

    UIImage * btn_URL = [btn_URL setImage:buttonImage forState:UIControlStateNormal];

    btn_URL.frame = CGRectMake(x, y, buttonImage.size.width, buttonImage.size.height);

    [btn_URL addTarget:self action:@selector(buttonAction:) forControlEvents:UIControlEventTouchUpInside];

    btn_URL.tag = Tag;

    [scrollview addSubview:btn_URL];
}

-(void)buttonAction:(id)sender {
    NSURL *URL = [NSURL URLWithString:[NSString stringWithFormat:@"%@",[array objectAtIndex:[sender Tag]]]];

}

您可以在此处将用户重定向到网址。

如果您需要任何帮助,请告知我们。

谢谢,

Hemang。