[英]How to reverse movement of a UIView?
我有一個uiview,它堆疊在另一個在左側來回滑動的uiview的頂部。 我怎樣才能扭轉它,使其在右側滑動? 這是我正在使用的代碼:
//Slides Left
- (IBAction) panLayer:(UIPanGestureRecognizer *)pan {
if (pan.state == UIGestureRecognizerStateChanged) {
CGPoint point = [pan translationInView:self.topLayer];
CGRect frame = self.topLayer.frame;
frame.origin.x = self.layerPosition +point.x;
if (frame.origin.x < 0) frame.origin.x = 0;
self.topLayer.frame = frame;
}
if (pan.state ==UIGestureRecognizerStateEnded) {
if (self.topLayer.frame.origin.x <= 160) {
[self animateLayerToPoint:0];
} else {
[self animateLayerToPoint:VIEW_HIDDEN];
}
}
}
// Slides right
- (IBAction)panLayerRight:(UIPanGestureRecognizer *)pan2
{
if(pan2.state == UIGestureRecognizerStateChanged)
{
CGPoint point = [pan2 translationInView:topLayer];
CGRect frame = topLayer.frame;
frame.origin.x = layerPosition + point.x;
if(frame.origin.x + 320 > 320) frame.origin.x = 0;
topLayer.frame = frame;
}
if (pan2.state == UIGestureRecognizerStateEnded) {
if (topLayer.frame.origin.x +320 <= 260) {
[self animateLayerToPoint2:-180];
}
else
{
[self animateLayerToPoint2:VIEW_HIDDEN];
}
}
}
-(void) animateLayerToPoint:(CGFloat)x
{
[UIView animateWithDuration:0.3
delay:0
options:UIViewAnimationCurveEaseOut
animations:^{
CGRect frame = self.topLayer.frame;
frame.origin.x = x;
self.topLayer.frame = frame;
}
completion:^(BOOL finished){
self.layerPosition = self.topLayer.frame.origin.x;
}];
}
-(void)animateLayerToPoint2:(CGFloat)x
{
[UIView animateWithDuration:0.3
delay:0
options:UIViewAnimationOptionCurveEaseOut
animations:^{
CGRect frame = topLayer.frame;
frame.origin.x = x;
topLayer.frame = frame;
}completion:^(BOOL finished) {
self.layerPosition = topLayer.frame.origin.x;
}];
}
- (IBAction)panLayer:(UIPanGestureRecognizer *)pan
{
if(pan.state == UIGestureRecognizerStateChanged)
{
CGPoint point = [pan translationInView:topLayer];
CGRect frame = topLayer.frame;
frame.origin.x = layerPosition + point.x;
if(frame.origin.x + 320 > 320) frame.origin.x = 0;
topLayer.frame = frame;
}
if (pan.state == UIGestureRecognizerStateEnded) {
if (topLayer.frame.origin.x +320 <= 260) {
[self animateLayerToPoint:-180];
}
else
{
[self animateLayerToPoint:0];
}
}
}
-(void)animateLayerToPoint:(CGFloat)x
{
[UIView animateWithDuration:0.3
delay:0
options:UIViewAnimationOptionCurveEaseOut
animations:^{
CGRect frame = topLayer.frame;
frame.origin.x = x;
topLayer.frame = frame;
}completion:^(BOOL finished) {
self.layerPosition = topLayer.frame.origin.x;
}];
}
希望這可以幫助...
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.