如何在objective-c中按照您的手指制作图像?

时间:2010-08-10 01:54:42

标签: iphone objective-c

如何在objective-c中按照您的手指制作图像?

3 个答案:

答案 0 :(得分:14)

  • 创建您的UIView并对其进行配置(或其任何子类,例如UIImageView
  • 将图像的位置设置为用户触摸的位置:

四个委托方法用于接受触摸事件,这些方法是从UIResponder继承的任何类的一部分,例如UIView。使用最适合您的委托方法。如果您希望它遵循您的手指,这将是-touchesMoved

- (void) touchesMoved:(NSSet*)toucheswithEvent:(UIEvent*)event {        
    CGPoint pt = [[touches anyObject] locationInView:self];
    myImageView.center = pt;
}

您可以使用的其他委托方法

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

我写了一个示例应用程序,它完全符合您的要求。这是一个绘制Quartz 2D图形的演示,但它会绘制一个红色正方形和黑色圆圈,您可以拖动手指,并且应该足够简单:

alt text http://brockwoolf.com/shares/stackoverflow/3445494/screen.png

Download Xcode project(32kb)

答案 1 :(得分:1)

这里有一篇很棒的帖子。

Divan Visagie

这是相关代码(取自上面的链接):

- (void)viewDidLoad
{
    [super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.

    //Find the path for the menu resource and load it into the menu array
    NSString *menuPlistPath = [[NSBundle mainBundle] pathForResource:@"Menu" ofType:@"plist"];

    menuArray = [[NSArray alloc] initWithContentsOfFile:menuPlistPath];

    //add some gestures
//    UISwipeGestureRecognizer *swipeLeft = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(handleSwipeLeft:)];
//    [swipeLeft setDirection:UISwipeGestureRecognizerDirectionLeft];
    //[self.view addGestureRecognizer:swipeLeft];

//    UISwipeGestureRecognizer *swipeRight = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(handleSwipeRight:)];
//    [swipeRight setDirection:UISwipeGestureRecognizerDirectionRight];
    //[self.view addGestureRecognizer:swipeRight];

}



float difference;
-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event{
    CGPoint contentTouchPoint = [[touches anyObject] locationInView:content];
    difference = contentTouchPoint.x;
}


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

    CGPoint pointInView = [[touches anyObject] locationInView:self.view];

    float xTarget = pointInView.x - difference;
    if(xTarget > menuTable.frame.size.width)
        xTarget = menuTable.frame.size.width;
    else if( xTarget < 0)
        xTarget = 0;

    [UIView animateWithDuration:.25
                     animations:^{

                         [content setFrame:CGRectMake(xTarget, content.frame.origin.y, content.frame.size.width, content.frame.size.height)];
                     }
     ];
}


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


    CGPoint endPoint = [[touches anyObject] locationInView:self.view];
    float xTarget = endPoint.x - difference;
    if(xTarget < (menuTable.frame.size.width/2))
        xTarget = 0;
    else
        xTarget = menuTable.frame.size.width;

    [UIView animateWithDuration:.25
                     animations:^{

                         [content setFrame:CGRectMake(xTarget, content.frame.origin.y, content.frame.size.width, content.frame.size.height)];
                     }
     ];
}

答案 2 :(得分:0)

Apple Sample Code库附带一个编写良好的示例,名为Touches。它还演示了iOS 4.0中的新UIGestureRecognizers功能。

相关问题