[英]iOS Animate sliding an imageview from off-screen to on-screen with gesture
我正在尋找動畫氣泡,上面有文字可以在屏幕上滑動。 這個動畫的理想實現是iOS的地平線滾動,啟用了分頁。 當我到達講話氣泡的末尾時,我肯定想要“反彈”,我明確地希望氣泡能夠跟蹤手指,直到它們滑出屏幕之前的某一點。 我認為這與滑動不同(這只是一個方向的輕彈)。
但是,水平滾動的問題在於它針對靜態數量的圖像進行了優化。 我將擁有動態數量的圖像,據我所知,您無法動態地將圖像附加到水平滾動條。 我們的想法是,當您繼續通過它時,應用程序會動態地向滾動條添加內容。
卷軸很容易上手但我現在不得不拆掉它。 我怎樣才能開始使用手勢(我不確定此時標准手勢識別器是否適用於我)以及動畫? 我之前從未使用過那部分iOS代碼。
我不確定我是否完全按照你的問題,但如果你想根據手勢動畫一些東西,你可以使用UIPanGestureRecognizer
並改變你想要的UIPanGestureRecognizer
視圖的center
。 例如,在viewDidLoad
您將:
UIPanGestureRecognizer *panGesture = [[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(movePiece:)];
[whateverViewYouWantToAnimate addGestureRecognizer:panGesture];
然后,您可以讓手勢識別器將其移動到您想要的位置:
- (void)movePiece:(UIPanGestureRecognizer *)gestureRecognizer
{
static CGPoint originalCenter;
if (gestureRecognizer.state == UIGestureRecognizerStateBegan)
{
originalCenter = [gestureRecognizer view].center;
}
else if (gestureRecognizer.state == UIGestureRecognizerStateChanged)
{
CGPoint translation = [gestureRecognizer translationInView:self.view];
gestureRecognizer.view.center = CGPointMake(originalCenter.x + translation.x, originalCenter.y);
// if you wanted to animate both left/right and up/down, it would be:
// gestureRecognizer.view.center = CGPointMake(originalCenter.x + translation.x, originalCenter.y + translation.y);
}
else if (gestureRecognizer.state == UIGestureRecognizerStateEnded)
{
// replace this offscreen CGPoint with something that makes sense for your app
CGPoint offscreen = CGPointMake(480, gestureRecognizer.view.center.y);
[UIView animateWithDuration:0.5
animations:^{
gestureRecognizer.view.center = offscreen;
}
completion:^(BOOL finished){
// when you're done, you might want to do whatever cleanup
// is appropriate for your app (e.g. do you want to remove it?)
[gestureRecognizer.view removeFromSuperview];
}];
}
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.