0

我在滚动视图下有图像。Awesomeview 有 3 张图片。当我单击按钮时,真棒视图转到中心。如何从视图中心移动特定图像。当我使用 UIPanGestureRecognizer 时,有 3 个图像在移动。我无法移动特定图像

代码:

scroll = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 500, self.view.frame.size.width, self.view.frame.size.height)];
        scroll.pagingEnabled = YES;
        NSInteger numberOfViews = 3;
        for (int i = 0; i < numberOfViews; i++) {
          //  CGFloat xOrigin = i * self.view.frame.size.width;
           // awesomeView = [[UIView alloc] initWithFrame:CGRectMake(xOrigin, 0, self.view.frame.size.width, self.view.frame.size.height)];

            awesomeView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 320, self.view.frame.size.height)];

            awesomeView.backgroundColor=[UIColor clearColor];
            [scroll addSubview:awesomeView];

            imagView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 100,100)];
            [imagView setImage:[UIImage imageNamed:@"img.png"]];
            [awesomeView addSubview:imagView];
            [imagView setTag:0];

            [imagView setUserInteractionEnabled:YES];


             imagView1 = [[UIImageView alloc] initWithFrame:CGRectMake(110, 0, 100,100)];
            [imagView1 setImage:[UIImage imageNamed:@"img1.png"]];
            [awesomeView addSubview:imagView1];
            [imagView1 setTag:1];

            imagView2 = [[UIImageView alloc] initWithFrame:CGRectMake(210, 0, 100,100)];
            [imagView2 setImage:[UIImage imageNamed:@"img10.png"]];
            [awesomeView addSubview:imagView2];
            [imagView2 setTag:2];




          vc=[[UIView alloc]initWithFrame:CGRectMake(self.view.frame.size.width, 0, self.view.frame.size.width, self.view.frame.size.height)];
            [scroll addSubview:vc];

            UIButton *men = [UIButton buttonWithType:UIButtonTypeCustom];

            [men setImage:[UIImage imageNamed:@"img2.png"] forState:UIControlStateNormal];
            // [overlayButton setFrame:CGRectMake(80, 420, 60, 30)];

            [men setFrame:CGRectMake(10,0,100,100)];

            //  [arrowButton addTarget:self action:@selector(ButtonPressed:) forControlEvents:UIControlEventTouchUpInside];
            [vc addSubview:men];

            UIButton *departments = [UIButton buttonWithType:UIButtonTypeCustom];

            [departments setImage:[UIImage imageNamed:@"img3.png"] forState:UIControlStateNormal];
            // [overlayButton setFrame:CGRectMake(80, 420, 60, 30)];

            [departments setFrame:CGRectMake(120,0,100,100)];

            //  [arrowButton addTarget:self action:@selector(ButtonPressed:) forControlEvents:UIControlEventTouchUpInside];
            [vc addSubview:departments];

            UIButton *blog = [UIButton buttonWithType:UIButtonTypeCustom];

            [blog setImage:[UIImage imageNamed:@"sofa3.png"] forState:UIControlStateNormal];
            // [overlayButton setFrame:CGRectMake(80, 420, 60, 30)];

            [blog setFrame:CGRectMake(230,0,100,100)];

            //  [arrowButton addTarget:self action:@selector(ButtonPressed:) forControlEvents:UIControlEventTouchUpInside];
            [vc addSubview:blog];




        }
        scroll.contentSize = CGSizeMake(self.view.frame.size.width * numberOfViews, self.view.frame.size.height);
        [self.view addSubview:scroll];

}];

按钮点击:

-(void)button:(id)sender{

    NSLog(@"press");

  [myImageView8 addSubview:awesomeView];


}

UIPanGesture:

myImageView8=[[UIImageView alloc]initWithFrame:CGRectMake(20, 150, 170, 150)];
    [self.view addSubview:myImageView8];
    myImageView8.userInteractionEnabled=YES;

UIPanGestureRecognizer *pan8=[[UIPanGestureRecognizer alloc]initWithTarget:self action:@selector(onPan8:)];

    [myImageView8 addGestureRecognizer:pan8];

- (void)onPan8:(UIPanGestureRecognizer *)pan {
    //  CGPoint offset = [pan translationInView:self.view];
    CGPoint offset = [pan translationInView:myImageView8];

    CGPoint center = pan.view.center;
    center.x += offset.x;
    center.y += offset.y;
    pan.view.center = center;

    [pan setTranslation:CGPointZero inView:myImageView8];
}
4

1 回答 1

0

将 ContentOffset 值设置为滚动视图,然后它会自动移动到该位置

[scroll setContentOffset:CGPointMake(320*index, 0) animated:YES];//index is an integer type value
于 2013-06-11T13:11:16.387 回答