ios - 平移 uiview 在触摸方向移动,iphone
<p><p>我想将 <code>UIView</code> 平移到与触摸移动相同的方向。</p>
<p>请提出建议。</p></p>
<br><hr><h1><strong>Best Answer-推荐答案</ strong></h1><br>
<p><p>修改 touchesBegan 和 touchesMoved 方法如下</p>
<pre><code>float oldX, oldY;
BOOL dragging;
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
UITouch *touch = [ anyObject];
CGPoint touchLocation = ;
if (CGRectContainsPoint(window.frame, touchLocation)) {
dragging = YES;
oldX = touchLocation.x;
oldY = touchLocation.y;
}
}
- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event {
UITouch *touch = [ anyObject];
CGPoint touchLocation = ;
if (dragging) {
CGRect frame = window.frame;
frame.origin.x = window.frame.origin.x + touchLocation.x - oldX;
frame.origin.y =window.frame.origin.y + touchLocation.y - oldY;
window.frame = frame;
}
oldX = touchLocation.x;
oldY = touchLocation.y;
}
- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event {
dragging = NO;
}
</code></pre>
<p>希望对你有帮助</p></p>
<p style="font-size: 20px;">关于ios - 平移 uiview 在触摸方向移动,iphone,我们在Stack Overflow上找到一个类似的问题:
<a href="https://stackoverflow.com/questions/8254426/" rel="noreferrer noopener nofollow" style="color: red;">
https://stackoverflow.com/questions/8254426/
</a>
</p>
页:
[1]