移动特定图像

时间:2013-06-11 12:52:39

标签: objective-c cocoa-touch uiscrollview uiimageview core-animation

我在scrollview下有图片。真棒有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];
}

1 个答案:

答案 0 :(得分:0)

将ContentOffset值设置为scrollview,然后它会自动移动到位置

[scroll setContentOffset:CGPointMake(320*index, 0) animated:YES];//index is an integer type value