亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

如果我要做這種view跟著手指運動,還有什么其他的方案嗎?還是我這個用法用的不對?

如果我要做這種view跟著手指運動,還有什么其他的方案嗎?還是我這個用法用的不對?

iOS
jeck貓 2023-04-16 17:13:40
我用UIPanGestureRecognizer加在一個View上用來監聽手指的滑動,然后根據 translation 的值來修改view的frame,但是我發現這樣修改,view的動畫并不能實時反應,手指滑動快了,就會出現延遲現象。代碼如下:- (void)slidePanAction:(UIPanGestureRecognizer *)recognizer {    CGPoint translation = [recognizer translationInView:self.movingView];    if(recognizer.state == UIGestureRecognizerStateChanged) { // sliding.         self.movingView.frame = CGRectMake(self.origin.x + translation.x, self.origin.y + translation.y,                                           self.movingView.frame.size.width, self.movingView.frame.size.height);     }    else if(recognizer.state == UIGestureRecognizerStateEnded) { // end slide.         self.origin = CGPointMake(self.movingView.frame.origin.x, self.movingView.frame.origin.y);     } }- (void)viewDidLoad {     [super viewDidLoad];         self.movingView = [[UILabel alloc] initWithFrame:CGRectMake(10.0f, 10.0f, 100.0f, 100.0f)];    self.movingView.backgroundColor = [UIColor redColor];     [self.view addSubview:self.movingView];    self.origin = CGPointMake(10.0f, 10.0f);    UIPanGestureRecognizer *panRecognizer = [[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(slidePanAction:)];     [self.view addGestureRecognizer:panRecognizer]; }
查看完整描述

1 回答

?
慕神8447489

TA貢獻1780條經驗 獲得超1個贊

可以在view中重寫

- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event

達成此目的。

另外gesture recognizer正常情況不會出現延遲。請檢查是否每次對位置的提交都發起了動畫(比如layer.position = newPosition默認情況下是有動畫的,在觸摸移動時設置layer.position且不關閉動畫,移動會有明顯延遲)


查看完整回答
反對 回復 2023-04-20
  • 1 回答
  • 0 關注
  • 170 瀏覽

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號