用手势滑动视图
问题描述:
我正在尝试开发一个应用程序给Iphone;但我是新的蜜蜂,所以我有很多问题。用手势滑动视图
我的应用程序有一个默认视图和主视图中名称为flashcard的另一个小视图。
我想增加这个视图在Iphone中像照片一样滑动(或滑动)的功能。例如,如果用户滑动视图直到内部视图的中心到达主视图的边界,则将有两种可能性,1 - 如果用户在到达内部视图之前完成滑动,则会返回原始位置。 012-2-如果用户不停止滑动,内部视图将从滑动方向在屏幕上熄灭,并从相反方向返回屏幕。
所以我宣布UIPanGestureRecognizer在viewDidLoad中像下面
UIPanGestureRecognizer *panFlashCard = [[UIPanGestureRecognizer alloc]
initWithTarget:self action:@selector(handlePanFlashCard:)];
[flashcard addGestureRecognizer:panFlashCard];
[panFlashCard release];
handPanFlashCard行动:
UIView *piece = [recognizer view];
CGPoint center = piece.center;
CGFloat x = 160; // the original x axis of inner view
if ([recognizer state] == UIGestureRecognizerStateChanged) {
CGPoint translation = [recognizer translationInView:piece ];
[UIView beginAnimations:nil context:NULL];
[UIView setAnimationDuration:0.55];
[piece setCenter:CGPointMake(piece.center.x + translation.x, center.y)];
[UIView commitAnimations];
CGFloat totalX;
totalX= x + translation.x;
if(translation.x <0)
{
if (totalX <= 0)
{
[self showNextCard];
}
else
{
[UIView beginAnimations:nil context:NULL];
[UIView setAnimationDuration:0.55];
[piece setCenter:CGPointMake(self.view.center.x, center.y)];
[UIView commitAnimations];
}
}
else
{
if (totalX >= self.view.bounds.size.width)
{
[self showPreviousCard];
}
else
{
[UIView beginAnimations:nil context:NULL];
[UIView setAnimationDuration:0.55];
[piece setCenter:CGPointMake(self.view.center.x, center.y)];
[UIView commitAnimations];
}
}
}
showNextCard行动:
[flashcard setCenter:CGPointMake(self.view.bounds.size.width + flashcard.bounds.size.width, flashcard.center.y)];
[UIView beginAnimations:nil context:NULL];
[UIView setAnimationDuration:0.55];
[flashcard setCenter:CGPointMake(self.view.center.x, flashcard.center.y)];
[UIView commitAnimations];
showPreviousCard行动:
[flashcard setCenter:CGPointMake(0 - flashcard.bounds.size.width, flashcard.center.y)];
[UIView beginAnimations:nil context:NULL];
[UIView setAnimationDuration:0.55];
[flashcard setCenter:CGPointMake(self.view.center.x, flashcard.center.y)];
[UIView commitAnimations];
当我运行这个应用程序,它建立并运行成功,但有动画错误,我认为一些框架错误。
请帮我改正这段代码,并在滑动时运行流畅的动画。
答
如果我明白你正在尝试做什么,那么你可以使用内置的控件。查看UIScrollView类的pagingEnabled属性。还有一个名为PhotoScroller的示例项目可能有所帮助。
http://developer.apple.com/library/ios/#samplecode/PhotoScroller/Introduction/Intro.html
我会看样例代码;但我不确定这些控件适合我。因为我想在内部视图中添加一些标签并从sqlite数据库中获取新数据每次完整刷卡(NextCard或PreviousCard操作) – 2011-05-05 06:10:25
您可以将所需的任何视图添加到滚动视图。所以你可以将你的数组与每个卡作为一个拥有多个标签的UIView。然后,你只需要弄清楚哪张卡片正在显示,并进行加载。 – jaminguy 2011-05-05 21:59:47