Iphone 绑定添加到UIPangestureRecognitor的视图在其superview中的移动
我想知道在使用手势识别器时,是否有办法将UIView的移动绑定到其superview中 我有一个UIImageview,我给它添加了带有动作面板的UIPangestureRecognitor。我按以下方式调整其中心,以避免其从superview中移出。但是它不能正常工作Iphone 绑定添加到UIPangestureRecognitor的视图在其superview中的移动,iphone,uigesturerecognizer,Iphone,Uigesturerecognizer,我想知道在使用手势识别器时,是否有办法将UIView的移动绑定到其superview中 我有一个UIImageview,我给它添加了带有动作面板的UIPangestureRecognitor。我按以下方式调整其中心,以避免其从superview中移出。但是它不能正常工作 -(CGPoint) centerWithBounds:(CGPoint)center andViewFrame:(CGRect)viewFrame andBoundingFrame:(CGRect)boundingFrame{
-(CGPoint) centerWithBounds:(CGPoint)center andViewFrame:(CGRect)viewFrame andBoundingFrame:(CGRect)boundingFrame{
CGFloat lowerXBound = boundingFrame.origin.x + ( viewFrame.size.width/2 );
CGFloat higherXBound = boundingFrame.origin.x + boundingFrame.size.width - ( viewFrame.size.width/2 );
CGFloat lowerYBound = boundingFrame.origin.y + ( viewFrame.size.height/2 );
CGFloat higherYBound = boundingFrame.origin.y + boundingFrame.size.height - ( viewFrame.size.height/2 );
if ( center.x < lowerXBound) {
center.x = lowerXBound;
}
if ( center.x > higherXBound ){
center.x = higherXBound;
}
if ( center.y < lowerYBound) {
center.y = lowerYBound;
}
if ( center.y > higherYBound ){
center.y = higherYBound;
}
return center;
}
- (void)panPiece:(UIPanGestureRecognizer *)gestureRecognizer
{
UIView *piece = [gestureRecognizer view];
[self adjustAnchorPointForGestureRecognizer:gestureRecognizer];
if ([gestureRecognizer state] == UIGestureRecognizerStateBegan || [gestureRecognizer state] == UIGestureRecognizerStateChanged) {
CGPoint translation = [gestureRecognizer translationInView:[piece superview]];
CGPoint translatedCenter = CGPointMake([piece center].x + translation.x, [piece center].y + translation.y);
CGPoint center = [self centerWithBounds:translatedCenter andViewFrame:[piece frame] andBoundingFrame:[[piece superview] frame]];
[piece setCenter:center];
[gestureRecognizer setTranslation:CGPointZero inView:[piece superview]];
}
}
-(CGPoint)带边界的中心:(CGPoint)中心和视框:(CGRect)视框和边界框:(CGRect)边界框{
cgflowerxbound=boundingFrame.origin.x+(viewFrame.size.width/2);
CGFloat higherXBound=boundingFrame.origin.x+boundingFrame.size.width-(viewFrame.size.width/2);
CGFloat LOWERYBOND=boundingFrame.origin.y+(viewFrame.size.height/2);
CGFloat HIGHERYBOND=boundingFrame.origin.y+boundingFrame.size.height-(viewFrame.size.height/2);
if(中心x<较低x波段){
中心x=较低的x波;
}
如果(中心x>高x波段){
中心.x=高xbound;
}
if(中心y<下限){
中心y=下限;
}
如果(中心y>高边界){
中心y=高边界;
}
返回中心;
}
-(无效)装甲:(UIPangestureRecognitor*)手势识别器
{
UIView*piece=[gestureRecognizer视图];
[自调整主播点识别器:手势识别器];
如果([GestureRecognitzer state]==UIGestureRecognitzerStateStarted | |[GestureRecognitzer state]==UIGestureRecognitzerStateChanged){
CGPoint translation=[GestureRecognitor TranslationView:[piece superview]];
CGPoint translatedCenter=CGPointMake([piece center].x+translation.x[piece center].y+translation.y);
CGPoint center=[带边界的自中心:平移中心和视框:[工件框架]和边界框架:[[工件超视图]框架]];
[工件设置中心:中心];
[GestureRecognitor setTranslation:CGPointZero inView:[piece superview];
}
}
非常感谢代码帮助。可能是因为
标准UIImageView尽管是UIView子类,但不会对添加到其中的手势识别器做出反应
您只需将属性clipsToBounds(设置为“是”)用于superview。之前也有同样的问题。在那之后效果很好。这不是真的。您只需要在UIImageView上设置userInteractionEnabled=YES。