ios - How to restrict a moveable view by Pan gesture -
i have uiimageview
moveable via pan gesture.
uipangesturerecognizer *pan = [[uipangesturerecognizer alloc] initwithtarget:self action:@selector(handlepan:)]; [self.photomask addgesturerecognizer:pan];
i restrict area can moved on screen. rather user able drag view right side of screen, want restrict margin of sort. how can this?
also, how handled when rotated?
edit ---
#pragma mark - gesture recognizer -(void)handlepan:(uipangesturerecognizer *)gesture { nslog(@"pan gesture"); gesture.view.center = [gesture locationinview:self.view]; }
this current method handle pan. need continue move imageview center point , restrict movement when close edge of screen 50 example.
one possible solution in handlepan method, check location of point on screen, , commit change if within bounds wish restrict to.
for ex.
-(void) handlepan:(uigesturerecognizer*)panges{ cgpoint point = [panges locationinview:self.view]; //only allow movement within 100 pixels of right bound of screen if (point.x < [uiscreen mainscreen].bounds.size.width - 100) { cgrect newframe = cgrectmake(point.x, point.y, theimageview.frame.size.width, theimageview.frame.size.height); theimageview.frame = newframe; } }
i believe correctly handle screen rotation
edit
to move image view center of frame, handlepan
method this.
-(void)handlepan:(uipangesturerecognizer *)gesture { cgpoint point = [gesture locationinview:self.view]; //only allow movement within 50 pixels of bounds of screen //ex. (iphone 5) cgrect boundsrect = cgrectmake(50, 50, 220, 448); if (cgrectcontainspoint(boundsrect, point)) { imgview.center = point; } }
check whether point within desired bounds, , if so, set center of image view frame point.
Comments
Post a Comment