0
好吧,基本上這個代碼目前所做的是沿着Y軸上下拖動圖像,這取決於用戶拖動它的位置,並且它返回到原來的位置。我的問題是,如果有人不直接觸及UIImageView的中心,並開始拖動它會震動(非常不流暢)。無論何人觸摸UIImageView並開始拖動UIImageView稍微晃動直接觸摸觸摸事件的中心。當觸摸事件發生時UIImageView跳轉
我在想使用動畫只是爲了將它移動到圖像需要去的地方,還是有另一種方式?
我很抱歉,如果這是一種低效率的方式來做到這一點。我對IOS世界相當陌生。
這是我有:
-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
//Gets location of UIImageView.
self.originalFrame = self.foregroundImage.frame;
}
//This method is used for moving the UIImageView along the y axis depending on touch events.
-(void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event {
UITouch *touch = [[event allTouches] anyObject];
if([touch view]==self.foregroundImage) {
CGPoint location = [touch locationInView:self.view];
location.x=self.foregroundImage.center.x;
self.foregroundImage.center=location;
}
}
//This method sets the UIImageView back to its original position.
-(void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event {
CGRect newFrame = self.foregroundImage.frame;
newFrame.origin.y = self.originalFrame.origin.y;
[UIView animateWithDuration:1.1 animations:^{
self.foregroundImage.frame = newFrame;
}];
}
非常感謝!現在我可以看到它。這正是我想要的。再次感謝你! – user2873147