C4:将平移添加到除“自身"之外的对象上. [英] C4: Add panning to an object other than "self"

查看:126
本文介绍了C4:将平移添加到除“自身"之外的对象上.的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我观看了C4教程,该教程介绍了如何向对象添加平移手势,以及如何在平移完成后为其设置动画以使其返回其原始位置.我试图将其添加到三个单独的对象.到目前为止,我已经将其与一个对象一起使用以将其移动并将其重置为CGPoint,但是要使其正常工作,我必须将摇摄手势添加到自身"而不是对象上.作为参考,我几乎使用这里的代码:

I watched the C4 tutorial on adding a pan gesture to an object and animating it to return to its original position when the panning is finished. I'm trying to add this to three individual objects. I have it working with one object so far to move it and reset it to a CGPoint, but for it to work, I have to add the pan gesture to "self", not the object. For reference, I'm pretty much using the code from here:

http://www.c4ios.com/tutorials/interactionPanning

如果我将手势添加到对象本身,则可以平移,但随后将其留在最后一个触摸位置.但是,我假设将手势留在自我"上不仅会影响我想要移动的对象,而且我希望能够分别移动三个对象.

If I add the gesture to the object itself, sure, it pans around, but then it just leaves itself at the last touch location. However, I'm assuming that leaving the gesture on "self" will affect more than just the object I want to move, and I want to be able to move the three objects individually.

我对示例中使用的移动"方法进行了大致相同的修改:

I'm using roughly the same modification to the "move" method that's used in the example:

-(void)move:(UIPanGestureRecognizer *)recognizer { 
    [character move:recognizer]; 
    if (recognizer.state == UIGestureRecognizerStateEnded) { 
        [character setCenter: charStartOrigin]; 
    } 
} 

然后是生成该对象的新方法:

And then a new method to spawn the object:

-(void)createCharacters { 
    character = [C4Shape ellipse:charStart]; 
    [character addGesture:PAN name:@"pan" action:@"move:"]; 
    [self.canvas addShape:character]; 
}

推荐答案

您正在使用的示例链接是偷偷摸摸的.因为我知道画布上只会有一个对象,所以我知道可以使它看起来像在平移标签一样.如您所知,这不适用于多个对象.

The example link you are working from is sneaky. Since I knew that there was only going to be one object on the canvas I knew I could make it look like I was panning the label. This won't work for multiple objects, as you have already figured out.

要使不同的对象独立移动并识别它们何时完成拖动,您需要子类并赋予它们自己的能力".

To get different objects to move independently, and recognize when they are done being dragged, you need to subclass the objects and give them their own "abilities".

为此,我:

  1. 子类
  2. 将自定义行为添加到新类中
  3. 在画布上创建子类对象

每个步骤的代码如下:

您必须创建一个赋予自身某些行为的子类.由于您正在使用形状,因此我也采用这种方式.我将子类称为Character,其文件如下所示:

You have to create a subclass that gives itself some behaviour. Since you're working with shapes I have done it this way as well. I call my subclass Character, its files look like this:

Character.h

#import "C4Shape.h"

@interface Character : C4Shape
@property (readwrite, atomic) CGPoint startOrigin;
@end

我在形状上添加了属性,以便我可以设置其开始原点(即它将返回的点).

I have added a property to the shape so that I can set its start origin (i.e. the point to which it will return).

Character.m

#import "Character.h"

@implementation Character

-(void)setup {
    [self addGesture:PAN name:@"pan" action:@"move:"];
}

-(void)move:(UIGestureRecognizer *)sender {
    if(sender.state == UIGestureRecognizerStateEnded) {
        self.center = self.startOrigin;
    } else {
        [super move:sender];
    }
}

@end

在C4对象的子类中,setup的调用方式与对画布的调用方式相同...因此,这是我为该对象添加手势的地方.在调用newalloc/init后,安装程序将运行.

In a subclass of a C4 object, setup gets called in the same way as it does for the canvas... So, this is where I add the gesture for this object. Setup gets run after new or alloc/init are called.

move:方法是我要使用自定义行为覆盖的位置.在这种方法中,我捕获了手势识别器,并且如果它的状态为UIGestureRecognizerStateEnded,那么我想动画回到起始原点.否则,我希望它像move:那样,因此我只需调用运行默认move:方法的[super move:sender].

The move: method is where I want to override with custom behaviour. In this method I catch the gesture recognizer, and if it's state is UIGestureRecognizerStateEnded then I want to animate back to the start origin. Otherwise, I want it to move: like it should so I simply call [super move:sender] which runs the default move: method.

对子类就是这样.

我的工作区如下所示:

#import "C4WorkSpace.h"
//1
#import "Character.h"

@implementation C4WorkSpace {
    //2
    Character *charA, *charB, *charC;
}

-(void)setup {
    //3
    CGRect frame = CGRectMake(0, 0, 100, 100);

    //4
    frame.origin = CGPointMake(self.canvas.width / 4 - 50, self.canvas.center.y - 50);
    charA = [self createCharacter:frame];
    frame.origin.x += self.canvas.width / 4.0f;
    charB = [self createCharacter:frame];
    frame.origin.x += self.canvas.width / 4.0f;
    charC = [self createCharacter:frame];

    //5
    [self.canvas addObjects:@[charA,charB,charC]];
}

-(Character *)createCharacter:(CGRect)frame {
    Character *c = [Character new];
    [c ellipse:frame];
    c.startOrigin = c.center;
    c.animationDuration = 0.25f;
    return c;
}

@end

我已经在我的工作区中添加了一个创建Character对象并将其添加到屏幕的方法.此方法通过调用其new方法创建一个Character对象(我必须这样做,因为它是C4Shape的子类),并使用我给它的框架将其变成椭圆形,设置其,更改其animationDuration.

I have added a method to my workspace that creates a Character object and adds it to the screen. This method creates a Character object by calling its new method (I have to do it this way because it is a subclass of C4Shape), turns it into an ellipse with the frame I gave it, sets its startOrigin, changes its animationDuration.

其余工作空间是怎么回事(注意:上面的代码中标记了这些步骤):

What's going on with the rest of the workspace is this (NOTE: the steps are marked in the code above):

  1. #import子类,以便我可以用它创建对象
  2. 我创建了3个对Character对象的引用.
  3. 我创建一个框架,用于构建每个新对象
  4. 对于每个对象,我通过更改其原点来重新放置frame的位置,然后使用它通过我编写的createCharacter:方法创建一个新对象.
  5. 我将所有新对象添加到canvas.
  1. I #import the subclass so that I can create objects with it
  2. I create 3 references to Character objects.
  3. I create a frame that I will use to build each of the new objects
  4. For each object, I reposition frameby changing its origin and then use it to create a new object with the createCharacter: method I wrote.
  5. I add all of my new objects to the canvas.

注意:因为我使用startOrigin属性创建了子类,所以我能够在该类中始终向后动画.我还可以随时在画布上设置该点.

NOTE: Because I created my subclass with a startOrigin property, I am able within that class to always animate back to that point. I am also able to set that point from the canvas whenever I want.

这篇关于C4:将平移添加到除“自身"之外的对象上.的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆