[英]how to make an object move by touching it in iOS
我是Objective-C的新手,正在尝试制作一个简单的应用程序,在该应用程序中,当您触摸对象时,它会随机移动一段时间并停止。 那么您需要再次触摸它,使其再次移动并过一会儿停止。
我已经搜索了触摸方法和一些教程,但是问题是我不知道如何开始。 就像我需要一个移动对象并触摸它的功能,但是我不知道如何连接和使用它们。
这是一个教程,它对我的功能很有帮助,并且实际上以与我的应用相反的方式起作用。 但是我仍然不能自己开始编程。
http://xcodenoobies.blogspot.se/2010/11/under-construction.html
对于如何编程逻辑,如何找到正确的方法以及如何找到所需变量的类型,您将获得任何帮助。
第1步:将此代码放入您的ViewDidLoad
方法中,在其中我创建了一些UIImageView
并将其随机添加到View中
[self.view setTag:1];
for(int i=0;i<4;i++)
{
int x = arc4random()%300;
int y = arc4random()%400;
#warning set Image here
UIImageView *imgview = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"someImage.png"]];
[imgview setFrame:CGRectMake(x, y, 25, 25)];
[imgview setUserInteractionEnabled:YES];
[self.view addSubview:imgview];
}
第2步:定义touchBegan
方法来处理触摸并在视图中移动对象,我们为ViewController
设置了Tag = 1,因为我们不想移动主视图,因此只会移动子视图
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
UITouch *touch = [touches anyObject];
if([[touch view] tag] != 1)
{
[UIView animateWithDuration:0.25f animations:^{
int x = arc4random()%300;
int y = arc4random()%400;
[[touch view] setCenter:CGPointMake(x, y)];
}];
}
}
您需要在想要触摸的视图中添加一个手势识别器:
// In a view controller or in a UIView subclass
UITapGestureRecognizer *tapGestureRecognizer = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(handleTap:)];
[self addGestureRecognizer:tapGestureRecognizer];
// Or [self.view addGestureRecognizer:tapGestureRecognizer];
- (void)handleTap:(UITapGestureRecognizer *)sender
{
if (sender.state == UIGestureRecognizerStateEnded) {
// Animate the view, move it around for a while etc.
// For animating a view use animateWithDuration:animations:
}
}
实际上,苹果为此做了一个演示。
http://developer.apple.com/library/ios/#samplecode/MoveMe/Introduction/Intro.html
您可以尝试根据需要修改此代码。 而实际的功能你在哪里寻找哪里:
- (void) touchesEnded:(NSSet*)touches withEvent:(UIEvent*)event
- (void) touchesMoved:(NSSet*)touches withEvent:(UIEvent*)event
- (void) touchesEnded:(NSSet*)touches withEvent:(UIEvent*)event
如果这是您要查找的答案,请单击“已回答”,这样该问题可以视为封闭的:-)。
如果我正确理解了,我会说,实现您所要求的最简单的方法是在Interface Builder中创建一个UIButton并将其连接到IBAction,该IBAction将其移动到随机位置。然后,您可以向按钮添加自定义图形..
我将/无法详细介绍特定代码,因为这将占用很多空间。 请注意,您的问题是非常开放和模糊的,在StackOverflow上,这不是好样式。 另外,除非您对iOS和Objective-C有所了解,否则您可能不希望保存动画等内容。
-V
是当用户触摸视图时调用的方法。 如果您在主视图(大视图)中覆盖此位置,则必须使用链接中所述的一些辅助方法来确定接触点是否是对象所在的位置。 否则,您可以覆盖对象的类并实现该方法,因此您不必显式地找到接触点是否在对象上。
根据您的要求。 我会说重写uiimageview&里面的touchesbegan实现,它将正常工作。
.h file
@interface StarView : UIImageView
@end
.m file
@implementation StarView
- (id)initWithFrame:(CGRect)frame
{
self = [super initWithFrame:frame];
if (self) {
// Initialization code
}
return self;
}
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
// your code in the link with the related methods
Destination = CGPointMake(arc4random() % 320, arc4random() % 480);
// calculate steps based on speed specified and distance between the current location of the sprite and the new destination
xamt = ((Destination.x - self.center.x) / speed);
yamt = ((Destination.y - self.center.y) / speed);
// ask timer to execute moveBall repeatedly each 0.2 seconds. Execute the timer.
mainTimer = [NSTimer scheduledTimerWithTimeInterval:(0.02) target:self selector:@selector(moveBall) userInfo:nil repeats: NO];
}
之后不要忘记复制moveBall方法。
在您的主视图中,只需创建一个StarView实例并添加到主视图中
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.