Reputation: 537
I'm making an draggable UIView which can move anywhere on screen. And I set it automatically hide 1/2 part of it after 3 seconds with this code:
-(void)hidePart {
if (self.frame.origin.x > 0) {
[UIView animateWithDuration:0.5 delay:0 options:UIViewAnimationOptionTransitionNone animations:^{
self.frame = CGRectMake(0 - self.frame.origin.x - self.frame.size.width/3, self.frame.origin.y, self.frame.size.width, self.frame.size.height);
} completion:^(BOOL finished) {
}];
}
}
It's work normally if I put UIView near left edge of screen. But if I drag and put it near right edge, after 3 secs it will run to left side and go out of screen. I know it's cause by I set the frame of it after 3 secs delay equal to: (current X - frame) and current X is very big in this case.
I wondering how can I make it better? I mean if this UIView is closer to left edge it will go out to left side and in case it's closer to the right edge, it will go out to right side?
Upvotes: 0
Views: 344
Reputation: 4038
Firstly, You should recognize that at which part of the view the draggable view is, left or right.
CGFloat pointLeft = CGRectGetMidX(self.frame);
BOOL isLeft = CGRectGetMidX(self.frame) < kScreenWidth/2;
Then, animate the view to left or right in terms of it. You have finished the left.
Upvotes: 1