Objective c 通过触摸控制动画
我有这种类型的代码Objective c 通过触摸控制动画,objective-c,iphone,core-animation,Objective C,Iphone,Core Animation,我有这种类型的代码 // create a UIImageView UIImageView *rollDiceImageMainTemp = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"rollDiceAnimationImage1.png"]]; // position and size the UIImageView rollDiceImageMainTemp.frame = CGRectMake(0, 0, 1
// create a UIImageView
UIImageView *rollDiceImageMainTemp = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"rollDiceAnimationImage1.png"]];
// position and size the UIImageView
rollDiceImageMainTemp.frame = CGRectMake(0, 0, 100, 100);
// create an array of images that will represent your animation (in this case the array contains 2 images but you will want more)
NSArray *savingHighScoreAnimationImages = [NSArray arrayWithObjects:
[UIImage imageNamed:@"rollDiceAnimationImage1.png"],
[UIImage imageNamed:@"rollDiceAnimationImage2.png"],
nil];
// set the new UIImageView to a property in your view controller
self.viewController.rollDiceImage = rollDiceImageMainTemp;
// release the UIImageView that you created with alloc and init to avoid memory leak
[rollDiceImageMainTemp release];
// set the animation images and duration, and repeat count on your UIImageView
[self.viewController.rollDiceImageMain setAnimationImages:savingHighScoreAnimationImages];
[self.viewController.rollDiceImageMain setAnimationDuration:2.0];
[self.viewController.rollDiceImageMain.animationRepeatCount:3];
// start the animation
[self.viewController.rollDiceImageMain startAnimating];
// show the new UIImageView
[self.viewController.view addSubview:self.rollDiceImageMain];
代替直接启动动画。有任何方法可以使用TouchsMoved来控制此代码吗?您可以使用UIResponder方法来检测触摸,如
-(void)TouchsBegind:(NSSet*)TouchsWithEvent:(UIEvent*)event
,您可以调用[self.viewController.rolldiemageMain startAnimating]代码>方法
一旦开始,你可以在一段时间后停止并限制接触
问候,
Satya实际上,您需要的是-(void)touchsmoved:(NSSet*)touchsetheevent:(UIEvent*)事件
。下面是允许UIView仅在x轴上移动的代码示例。适应您需要代码执行的任何操作
- (void)touchesMoved:(NSSet*)touches withEvent:(UIEvent*)event
{
CGPoint original = self.center;
UITouch* touch = [touches anyObject];
CGPoint location = [touch locationInView:self.superview];
// Taking the delta will give us a nice, smooth movement that will
// track with the touch.
float delta = location.x - original.x;
// We need to substract half of the width of the view
// because we are using the view's center to reposition it.
float maxPos = self.superview.bounds.size.width
- (self.frame.size.width * 0.5f);
float minPos = self.frame.size.width * 0.5f;
float intendedPos = delta + original.x;
// Make sure they can't move the view off-screen
if (intendedPos > maxPos)
{
intendedPos = maxPos;
}
// Make sure they can't move the view off-screen
if (intendedPos < minPos)
{
intendedPos = minPos;
}
self.center = CGPointMake(intendedPos, original.y);
// We want to cancel all other touches for the view
// because we don't want the touchInside event firing.
[self touchesCancelled:touches withEvent:event];
// Pass on the touches to the super
[super touchesMoved:touches withEvent:event];
}
-(无效)触摸移动:(NSSet*)触摸事件:(UIEvent*)事件
{
CGPoint original=自中心;
UITouch*touch=[触摸任何对象];
CGPoint location=[触摸位置视图:self.superview];
//采取三角洲将给我们一个很好的,平稳的运动,这将
//用触摸追踪。
浮动增量=位置.x-原始.x;
//我们需要减去视图宽度的一半
//因为我们正在使用视图的中心来重新定位它。
float maxPos=self.superview.bounds.size.width
-(自身框架、尺寸、宽度*0.5f);
float minPos=self.frame.size.width*0.5f;
float intendepos=delta+original.x;
//确保他们不能将视图移出屏幕
if(intendepos>maxPos)
{
intendepos=maxPos;
}
//确保他们不能将视图移出屏幕
如果(预期位置<最小位置)
{
intendepos=minPos;
}
self.center=CGPointMake(intendedPos,original.y);
//我们要取消视图的所有其他触摸
//因为我们不想触发touchInside事件。
[已取消自触摸:触摸事件:事件];
//将触感传递给super
[超级触摸移动:触摸事件:事件];
}
注意,这里我用手指取运动的增量,而不是。如果你用手指跟踪,你会得到非常不稳定的行为,这是非常不受欢迎的。应用三角形将提供漂亮、流畅的视图运动,并通过触摸输入完美跟踪
更新:另外,对于那些想知道我为什么选择乘0.5f而不是乘2的人,ARM处理器在硬件上不支持除法,因此使用乘法会带来微小的性能提升。在程序的生命周期中,这种性能优化可能只会被调用几次,因此它可能不会有什么不同,但在拖动时,会多次调用此特定消息。因为在这种情况下,它可能值得倍增。我有一个图像视图,其中包含一系列数组中的图像。。。我如何使用它?I’我是个菜鸟我为我的愚蠢道歉questions@user462533您将为数组中的每个对象使用UIImage或UIImageView子类,并将此方法添加到其中。默认情况下,UIView类在此方法中不执行任何操作,这就是您需要重写它的原因。