拖动、缩放和旋转多个 UIImageview

发布于 2024-11-04 02:40:54 字数 3730 浏览 3 评论 0原文

到目前为止,我可以移动、缩放和旋转所有对象,但多个对象都会一起移动。我想让他们分开搬。我想我必须将 objectatIndex 更改为 1,但它只是崩溃了。

我使用了以下代码:

- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {

    NSArray *allTouches = [touches allObjects];

    UITouch* t;
    if([[event allTouches] count]==1){
        if (CGRectContainsPoint([Birdie frame], [[allTouches objectAtIndex:0] locationInView:theimageView]) && CGRectContainsPoint([imageViewauto frame], [[allTouches objectAtIndex:0] locationInView:theimageView])) {
            t=[[[event allTouches] allObjects] objectAtIndex:0];
            touch1=[t locationInView:nil];
        }
    }else{
        t=[[[event allTouches] allObjects] objectAtIndex:0];
        touch1=[t locationInView:nil];
        t=[[[event allTouches] allObjects] objectAtIndex:1];
        touch2=[t locationInView:nil];
    }
}

-(double)distance:(CGPoint)point1 toPoint:(CGPoint)point2
{
    double deltaX, deltaY;
    deltaX = point1.x - point2.x;
    deltaY = point1.y - point2.y;
    return sqrt(deltaX * deltaX + deltaY * deltaY);
}

- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event {

    CGPoint currentTouch1;
    CGPoint currentTouch2;
    NSArray *allTouches = [touches allObjects];
    UITouch* t;
    float scale,rotation;

    if([[event allTouches] count]==1){
        t=[[[event allTouches] allObjects] objectAtIndex:0];
        if (CGRectContainsPoint([Birdie frame], [[allTouches objectAtIndex:0] locationInView:theimageView]) && CGRectContainsPoint([imageViewauto frame], [[allTouches objectAtIndex:0] locationInView:theimageView]))
        { 
            touch2=[t locationInView:nil];
            Birdie.center=CGPointMake(Birdie.center.x+touch2.x-touch1.x,Birdie.center.y+touch2.y-touch1.y);
            imageViewauto.center=CGPointMake(imageViewauto.center.x+touch2.x-touch1.x,imageViewauto.center.y+touch2.y-touch1.y);
            touch1=touch2;
        }
    }
    else if([[event allTouches] count]==2)
    {
        t=[[[event allTouches] allObjects] objectAtIndex:0];
        currentTouch1=[t locationInView:nil];

        t=[[[event allTouches] allObjects] objectAtIndex:1];
        currentTouch2=[t locationInView:nil];

        double distance1 =  [self distance:currentTouch1 toPoint:currentTouch2];
        double distance2 = [self distance:touch1 toPoint:touch2];

        if (distance2 == 0)
        {
            //handle the case where distance is zero
        }
        else {
            scale =distance1 / distance2;}

        rotation=atan2(currentTouch2.y-currentTouch1.y, currentTouch2.x-currentTouch1.x)-atan2(touch2.y-touch1.y,touch2.x-touch1.x);
        if(isnan(scale)){
            scale=1.0f;
        }
        NSLog(@"rotation %f",rotation);

        NSLog(@"scale %f",scale);

        if (CGRectContainsPoint([Birdie frame], [[allTouches objectAtIndex:0] locationInView:theimageView]) && CGRectContainsPoint([imageViewauto frame], [[allTouches objectAtIndex:0] locationInView:theimageView]))
        {

            Birdie.transform=CGAffineTransformScale(Birdie.transform, scale,scale);
            Birdie.transform=CGAffineTransformRotate(Birdie.transform, rotation);

            imageViewauto.transform=CGAffineTransformScale(imageViewauto.transform, scale,scale);
            imageViewauto.transform=CGAffineTransformRotate(imageViewauto.transform, rotation);
        }
        else // In case of scaling or rotating the background imageView
        {
            imageView.transform=CGAffineTransformScale(imageView.transform, scale,scale);
            imageView.transform=CGAffineTransformRotate(imageView.transform, rotation);
        }

        touch1=currentTouch1;
        touch2=currentTouch2;
    }
}

I've got it so far that I can move, scale and rotate all the objects, but the multiple objects all move together. I want them to move seperate. I guess I have to change the objectatIndex to 1, but it just crashes.

I've used the following code:

- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {

    NSArray *allTouches = [touches allObjects];

    UITouch* t;
    if([[event allTouches] count]==1){
        if (CGRectContainsPoint([Birdie frame], [[allTouches objectAtIndex:0] locationInView:theimageView]) && CGRectContainsPoint([imageViewauto frame], [[allTouches objectAtIndex:0] locationInView:theimageView])) {
            t=[[[event allTouches] allObjects] objectAtIndex:0];
            touch1=[t locationInView:nil];
        }
    }else{
        t=[[[event allTouches] allObjects] objectAtIndex:0];
        touch1=[t locationInView:nil];
        t=[[[event allTouches] allObjects] objectAtIndex:1];
        touch2=[t locationInView:nil];
    }
}

-(double)distance:(CGPoint)point1 toPoint:(CGPoint)point2
{
    double deltaX, deltaY;
    deltaX = point1.x - point2.x;
    deltaY = point1.y - point2.y;
    return sqrt(deltaX * deltaX + deltaY * deltaY);
}

- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event {

    CGPoint currentTouch1;
    CGPoint currentTouch2;
    NSArray *allTouches = [touches allObjects];
    UITouch* t;
    float scale,rotation;

    if([[event allTouches] count]==1){
        t=[[[event allTouches] allObjects] objectAtIndex:0];
        if (CGRectContainsPoint([Birdie frame], [[allTouches objectAtIndex:0] locationInView:theimageView]) && CGRectContainsPoint([imageViewauto frame], [[allTouches objectAtIndex:0] locationInView:theimageView]))
        { 
            touch2=[t locationInView:nil];
            Birdie.center=CGPointMake(Birdie.center.x+touch2.x-touch1.x,Birdie.center.y+touch2.y-touch1.y);
            imageViewauto.center=CGPointMake(imageViewauto.center.x+touch2.x-touch1.x,imageViewauto.center.y+touch2.y-touch1.y);
            touch1=touch2;
        }
    }
    else if([[event allTouches] count]==2)
    {
        t=[[[event allTouches] allObjects] objectAtIndex:0];
        currentTouch1=[t locationInView:nil];

        t=[[[event allTouches] allObjects] objectAtIndex:1];
        currentTouch2=[t locationInView:nil];

        double distance1 =  [self distance:currentTouch1 toPoint:currentTouch2];
        double distance2 = [self distance:touch1 toPoint:touch2];

        if (distance2 == 0)
        {
            //handle the case where distance is zero
        }
        else {
            scale =distance1 / distance2;}

        rotation=atan2(currentTouch2.y-currentTouch1.y, currentTouch2.x-currentTouch1.x)-atan2(touch2.y-touch1.y,touch2.x-touch1.x);
        if(isnan(scale)){
            scale=1.0f;
        }
        NSLog(@"rotation %f",rotation);

        NSLog(@"scale %f",scale);

        if (CGRectContainsPoint([Birdie frame], [[allTouches objectAtIndex:0] locationInView:theimageView]) && CGRectContainsPoint([imageViewauto frame], [[allTouches objectAtIndex:0] locationInView:theimageView]))
        {

            Birdie.transform=CGAffineTransformScale(Birdie.transform, scale,scale);
            Birdie.transform=CGAffineTransformRotate(Birdie.transform, rotation);

            imageViewauto.transform=CGAffineTransformScale(imageViewauto.transform, scale,scale);
            imageViewauto.transform=CGAffineTransformRotate(imageViewauto.transform, rotation);
        }
        else // In case of scaling or rotating the background imageView
        {
            imageView.transform=CGAffineTransformScale(imageView.transform, scale,scale);
            imageView.transform=CGAffineTransformRotate(imageView.transform, rotation);
        }

        touch1=currentTouch1;
        touch2=currentTouch2;
    }
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

此岸叶落 2024-11-11 02:40:54

首先,处理拖动、缩放和旋转的一种简单方法是使用 GestureRecognizers,如下所示:

    UIPinchGestureRecognizer *pinchRecognizer = [[UIPinchGestureRecognizer alloc] initWithTarget:self action:@selector(scale:)];
[pinchRecognizer setDelegate:self];
[self.view addGestureRecognizer:pinchRecognizer];

UIRotationGestureRecognizer *rotationRecognizer = [[UIRotationGestureRecognizer alloc] initWithTarget:self action:@selector(rotate:)];
[rotationRecognizer setDelegate:self];
[self.view addGestureRecognizer:rotationRecognizer];

UIPanGestureRecognizer *panRecognizer = [[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(move:)];
[panRecognizer setMinimumNumberOfTouches:1];
[panRecognizer setMaximumNumberOfTouches:1];
[panRecognizer setDelegate:self];
[self.view addGestureRecognizer:panRecognizer];

UITapGestureRecognizer *tapRecognizer = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapped:)];
[tapRecognizer setNumberOfTapsRequired:1];
[tapRecognizer setDelegate:self];
[self.view addGestureRecognizer:tapRecognizer];

要处理多个 UIImageView,您可以使用触摸点的位置,您可以使用以下函数来执行此操作:

- (CGPoint)locationInView:(UIView*)view;   
- (CGPoint)locationOfTouch:(NSUInteger)touchIndex inView:(UIView*)view;

根据数量对于触摸,您可以使用第一个或第二个函数,然后查看触摸点是否在所考虑的 UIImageView 的框架内,例如,对于缩放,您可以执行如下所示的操作:

-(void)scale:(id)sender {

UIView * pinchView = [(UIPinchGestureRecognizer*)sender view];

CGPoint first_point = [sender locationOfTouch:0 inView:pinchView];
CGPoint second_point = [sender locationOfTouch:1 inView:pinchView];


if (CGRectContainsPoint(my_image_view.frame, first_point) && CGRectContainsPoint(my_image_view.frame, second_point)) {
    [self.view bringSubviewToFront:pinchView];

    if([(UIPinchGestureRecognizer*)sender state] == UIGestureRecognizerStateEnded) {

        lastScale = 1.0;
        return;
    }

    CGFloat scale = 1.0 - (lastScale - [(UIPinchGestureRecognizer*)sender scale]);

    CGAffineTransform currentTransform = my_image_view.transform;
    CGAffineTransform newTransform = CGAffineTransformScale(currentTransform, scale, scale);
    [my_image_view setTransform:newTransform];

    lastScale = [(UIPinchGestureRecognizer*)sender scale];}}

First of all, an easy way to handle drag, scale and rotate is to use GestureRecognizers like the bellow one:

    UIPinchGestureRecognizer *pinchRecognizer = [[UIPinchGestureRecognizer alloc] initWithTarget:self action:@selector(scale:)];
[pinchRecognizer setDelegate:self];
[self.view addGestureRecognizer:pinchRecognizer];

UIRotationGestureRecognizer *rotationRecognizer = [[UIRotationGestureRecognizer alloc] initWithTarget:self action:@selector(rotate:)];
[rotationRecognizer setDelegate:self];
[self.view addGestureRecognizer:rotationRecognizer];

UIPanGestureRecognizer *panRecognizer = [[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(move:)];
[panRecognizer setMinimumNumberOfTouches:1];
[panRecognizer setMaximumNumberOfTouches:1];
[panRecognizer setDelegate:self];
[self.view addGestureRecognizer:panRecognizer];

UITapGestureRecognizer *tapRecognizer = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapped:)];
[tapRecognizer setNumberOfTapsRequired:1];
[tapRecognizer setDelegate:self];
[self.view addGestureRecognizer:tapRecognizer];

To handle multiple UIImageViews you may use the position of touched points, you can use the bellow functions to do so:

- (CGPoint)locationInView:(UIView*)view;   
- (CGPoint)locationOfTouch:(NSUInteger)touchIndex inView:(UIView*)view;

According to the number of touched you can use either the first or second function, and then see if the touched point is inside the frame of considered UIImageView, for example for scaling you can do as what is show in bellow:

-(void)scale:(id)sender {

UIView * pinchView = [(UIPinchGestureRecognizer*)sender view];

CGPoint first_point = [sender locationOfTouch:0 inView:pinchView];
CGPoint second_point = [sender locationOfTouch:1 inView:pinchView];


if (CGRectContainsPoint(my_image_view.frame, first_point) && CGRectContainsPoint(my_image_view.frame, second_point)) {
    [self.view bringSubviewToFront:pinchView];

    if([(UIPinchGestureRecognizer*)sender state] == UIGestureRecognizerStateEnded) {

        lastScale = 1.0;
        return;
    }

    CGFloat scale = 1.0 - (lastScale - [(UIPinchGestureRecognizer*)sender scale]);

    CGAffineTransform currentTransform = my_image_view.transform;
    CGAffineTransform newTransform = CGAffineTransformScale(currentTransform, scale, scale);
    [my_image_view setTransform:newTransform];

    lastScale = [(UIPinchGestureRecognizer*)sender scale];}}
洒一地阳光 2024-11-11 02:40:54

它的工作

-(void)scale:(id)sender {

    if([(UIPinchGestureRecognizer*)sender state] == UIGestureRecognizerStateBegan) {
        _lastScale = 1.0;
    }

    CGFloat scale = 1.0 - (_lastScale - [(UIPinchGestureRecognizer*)sender scale]);

    CGAffineTransform currentTransform = self.transform;
    CGAffineTransform newTransform = CGAffineTransformScale(currentTransform, scale, scale);

    [self setTransform:newTransform];


    _lastScale = [(UIPinchGestureRecognizer*)sender scale];
    [self showOverlayWithFrame:self.frame];
}


-(void)rotate:(id)sender {

    if([(UIRotationGestureRecognizer*)sender state] == UIGestureRecognizerStateEnded) {

        _lastRotation = 0.0;

        return;
    }
    CGFloat rotation = 0.0 - (_lastRotation - [(UIRotationGestureRecognizer*)sender rotation]);

    CGAffineTransform currentTransform = self.transform;
    CGAffineTransform newTransform = CGAffineTransformRotate(currentTransform,rotation);

    [self setTransform:newTransform];
    _lastRotation = [(UIRotationGestureRecognizer*)sender rotation];

    [self showOverlayWithFrame:self.frame];
}


-(void)move:(id)sender {

    CGPoint translatedPoint = [(UIPanGestureRecognizer*)sender translationInView:self];

     if([(UIPanGestureRecognizer*)sender state] == UIGestureRecognizerStateBegan) {
         _firstX = [self center].x;
         _firstY = [self center].y;
    }

    translatedPoint = CGPointMake(_firstX+translatedPoint.x, _firstY+translatedPoint.y);

    [self setCenter:translatedPoint];


    [self showOverlayWithFrame:self.frame];

    NSLog(@"after move x= %f     y=%f "  , _firstX,_firstY);
}

its working

-(void)scale:(id)sender {

    if([(UIPinchGestureRecognizer*)sender state] == UIGestureRecognizerStateBegan) {
        _lastScale = 1.0;
    }

    CGFloat scale = 1.0 - (_lastScale - [(UIPinchGestureRecognizer*)sender scale]);

    CGAffineTransform currentTransform = self.transform;
    CGAffineTransform newTransform = CGAffineTransformScale(currentTransform, scale, scale);

    [self setTransform:newTransform];


    _lastScale = [(UIPinchGestureRecognizer*)sender scale];
    [self showOverlayWithFrame:self.frame];
}


-(void)rotate:(id)sender {

    if([(UIRotationGestureRecognizer*)sender state] == UIGestureRecognizerStateEnded) {

        _lastRotation = 0.0;

        return;
    }
    CGFloat rotation = 0.0 - (_lastRotation - [(UIRotationGestureRecognizer*)sender rotation]);

    CGAffineTransform currentTransform = self.transform;
    CGAffineTransform newTransform = CGAffineTransformRotate(currentTransform,rotation);

    [self setTransform:newTransform];
    _lastRotation = [(UIRotationGestureRecognizer*)sender rotation];

    [self showOverlayWithFrame:self.frame];
}


-(void)move:(id)sender {

    CGPoint translatedPoint = [(UIPanGestureRecognizer*)sender translationInView:self];

     if([(UIPanGestureRecognizer*)sender state] == UIGestureRecognizerStateBegan) {
         _firstX = [self center].x;
         _firstY = [self center].y;
    }

    translatedPoint = CGPointMake(_firstX+translatedPoint.x, _firstY+translatedPoint.y);

    [self setCenter:translatedPoint];


    [self showOverlayWithFrame:self.frame];

    NSLog(@"after move x= %f     y=%f "  , _firstX,_firstY);
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文