如何在滚动视图中获取当前图像标记?

时间:2017-01-03 06:49:44

标签: ios objective-c

//add the scrollview to the view
Friend_Request_Scroll = [[UIScrollView alloc] initWithFrame:CGRectMake(self.view.frame.size.width/2-60,70,120,140)];
[Friend_Request_Scroll setAlwaysBounceVertical:NO];
Friend_Request_Scroll.delegate=self;
Friend_Request_Scroll.pagingEnabled=YES;
[Friend_Request_Scroll setShowsHorizontalScrollIndicator:NO];
[Friend_Request_Scroll setShowsVerticalScrollIndicator:NO];
CGFloat xOrigin = 0;

for (int i = 0; i <[imageCollection count]; i++) {

    Back_Request_Scroll=[[UIView alloc]init];
    Back_Request_Scroll.frame=CGRectMake(xOrigin,Friend_Request_Scroll.frame.origin.y-135,120,140);
    Back_Request_Scroll.backgroundColor=[UIColor colorWithRed:244.0f/255.0f green:244.0f/255.0f blue:244.0f/255.0f alpha:0.5];
    Back_Request_Scroll.clipsToBounds=YES;
    [Friend_Request_Scroll addSubview:Back_Request_Scroll];

    image = [[UIImageView alloc] initWithFrame:
             CGRectMake(0, 0,
                        Back_Request_Scroll.frame.size.width,
                        Back_Request_Scroll.frame.size.height)];
    [image sd_setImageWithURL:[imageCollection objectAtIndex:i]                placeholderImage:[UIImage imageNamed:@""]];

    image.tag=i;
    [Back_Request_Scroll addSubview:image];
    xOrigin= xOrigin+120;
    image.userInteractionEnabled=YES;

}
Friend_Request_Scroll.contentSize = CGSizeMake(xOrigin,
                                            0);
[self.view addSubview:Friend_Request_Scroll];


swipeleft=[[UISwipeGestureRecognizer alloc]initWithTarget:self action:@selector(swipeleft:)];
swipeleft.direction=UISwipeGestureRecognizerDirectionLeft;
[image addGestureRecognizer:swipeleft];


swiperight=[[UISwipeGestureRecognizer alloc]initWithTarget:self action:@selector(swiperight:)];
swiperight.direction=UISwipeGestureRecognizerDirectionRight;
[image addGestureRecognizer:swiperight];


pageControl = [[UIPageControl alloc] init];
pageControl.frame = CGRectMake(self.view.frame.size.width/2-60,Friend_Request_Scroll.frame.size.height+Friend_Request_Scroll.frame.origin.y,image.frame.size.width,20);
pageControl.numberOfPages = [imageCollection count];
pageControl.currentPage = currentValue;
[pageControl addTarget:self action:@selector(changePage) forControlEvents:UIControlEventValueChanged];
[self.view addSubview:pageControl];

4 个答案:

答案 0 :(得分:2)

尝试此委托方法。此委托将告诉您滚动已完成。

- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView

答案 1 :(得分:1)

尝试使用此代码,使用recongizer对象视图标记。

- (void)viewDidLoad 
{
    [super viewDidLoad];
    scroll = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 0, 320, 480)];
    [self.view addSubview:scroll];

    NSInteger i;
    for (i=0; i<20; i++) 
    {
        UIImageView *imageView = [[UIImageView alloc] initWithFrame:CGRectMake(10, i*90 + i*15, 300, 90)];
        imageView.backgroundColor = [UIColor blackColor];
        imageView.userInteractionEnabled = YES;
        imageView.tag = i;

        NSLog(@“image Tag = %d”, imageView.tag);

        [scroll addSubview:imageView];

       UISwipeGestureRecognizer *recognizer;

    recognizer = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(handleSwipeFrom:)];
    [recognizer setDirection:(UISwipeGestureRecognizerDirectionRight | UISwipeGestureRecognizerDirectionDown | UISwipeGestureRecognizerDirectionLeft | UISwipeGestureRecognizerDirectionUp)];
        [imageView addGestureRecognizer: recognizer];

    }

    scroll.contentSize = CGSizeMake(320, 110*i);

}
-(void)handleSwipeFrom:(UISwipeGestureRecognizer *)recognizer {
    NSLog(@"Swipe received.");
 NSLog(@“image Tag = %d”, recognizer.view.tag);
}

<强>更新 不需要刷卡。

- (void)scrollViewDidScroll:(UIScrollView *)sender {
    if (sender == scroll) {
        int pageNum = (int)(scroll.contentOffset.x / scroll.frame.size.width);
        NSLog(@"%d",pageNum);
        //self.pagecontroller.currentPage =pageNum;
    }
}

- (void)viewDidLoad {
    [super viewDidLoad];
    scroll = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 64, self.view.frame.size.width, self.view.frame.size.height)];
    scroll.delegate = self;
   scroll.pagingEnabled = YES;
    [self.view addSubview:scroll];
    CGFloat currentXOffset = 0;
    NSInteger i;
    for (i=0; i<3; i++)
    {
         UIImageView *imageView = [[UIImageView alloc] initWithFrame:CGRectMake(currentXOffset, 100, self.view.frame.size.width, 200)];
        if (i == 0){
            imageView.backgroundColor = [UIColor blackColor];
        }else if (i == 1){
            imageView.backgroundColor = [UIColor orangeColor];

        }else if (i == 2){
            imageView.backgroundColor = [UIColor grayColor];

        }
        imageView.userInteractionEnabled = YES;
        imageView.tag = i;
        currentXOffset = currentXOffset + imageView.frame.size.width;

        [scroll addSubview:imageView];

//        UISwipeGestureRecognizer *recognizer;
//        
//        recognizer = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(handleSwipeFrom:)];
//        [recognizer setDirection:(UISwipeGestureRecognizerDirectionRight | UISwipeGestureRecognizerDirectionDown | UISwipeGestureRecognizerDirectionLeft | UISwipeGestureRecognizerDirectionUp)];
//        [imageView addGestureRecognizer: recognizer];

    }

    scroll.contentSize = CGSizeMake(currentXOffset, self.view.frame.size.height);
}

答案 2 :(得分:0)

向图像添加点按手势动作,您可以使用[发件人标签]

获取标签

答案 3 :(得分:0)

我有几个ImageView,每个都有标签,我有一个图像阵列。当用户点击图像然后我得到图像标记并对其采取行动。

 - (void)viewDidLoad 
{
    [super viewDidLoad];
    scroll = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 0, 320, 480)];
    [self.view addSubview:scroll];

    NSInteger i;
    for (i=0; i<20; i++) 
    {
        UIImageView *imageView = [[UIImageView alloc] initWithFrame:CGRectMake(10, i*90 + i*15, 300, 90)];
        imageView.backgroundColor = [UIColor blackColor];
        imageView.userInteractionEnabled = YES;
        imageView.tag = i;

        NSLog(@“image Tag = %d”, imageView.tag);

        [scroll addSubview:imageView];

        UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(findTheTag:)];
        [imageView addGestureRecognizer:tap];

    }

    scroll.contentSize = CGSizeMake(320, 110*i);

}
- (void)findTheTag:(id)sender
{

     NSLog(@“image Tag = %d”, sender.tag);


}