[英]Keeping the subview stationary, while superview is moving
我一直在嘗試構建自定義的uitableview滑動。 我在那方面很成功。 當它遇到touchesMoved時,我還想將uiview作為子視圖添加到tablecell。 當我嘗試移動表格單元時,我希望將uiview附加為固定的。 為此,我還要使它向opp方向移動(如StackOverflow的答案之一所示)。 但這並不能保持穩定。 子視圖也隨單元格一起移動。 請幫助我找出我要去哪里錯了? 代碼如下:
- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event
{
UITableViewCell * cell = (UITableViewCell *)[self cellForRowAtIndexPath:[self indexPathForRowAtPoint:gestureStartPoint]];
isTouchesMoved=YES;
if ([self supportsSwipingForCellAtPoint:gestureStartPoint])
{
[NSObject cancelPreviousPerformRequestsWithTarget:self selector:@selector(highlightTouchedRow) object:nil];
UITouch * touch = [touches anyObject];
CGPoint currentPosition = [touch locationInView:self];
CGFloat deltaX = fabsf(gestureStartPoint.x - currentPosition.x);
CGFloat deltaY = fabsf(gestureStartPoint.y - currentPosition.y);
if (deltaX >= kMinimumGestureLength && deltaY <= kMaximumVariance)
{
tempView=[self addnewViewatPoint:touches withEvent:event];
[cell addSubview:tempView];
[self setScrollEnabled:NO];
CGPoint loc = [touch locationInView:self];
CGPoint prevloc = [touch previousLocationInView:self];
if(moved==TRUE)
{
prevLoc=cell.frame;
prevLocBack=tempView.frame;
moved=FALSE;
}
CGRect myFrame = cell.frame;
CGRect backFrame = tempView.frame;
CGFloat deltaX = loc.x - prevloc.x;
if(gestureStartPoint.x>currentPosition.x)
{
NSLog(@"You have swiped left");
CGFloat movedDistance=gestureStartPoint.x-currentPosition.x;
NSLog(@"The movedDistance is %f",movedDistance);
if(movedDistance<=160)
{
myFrame.origin.x += deltaX;
backFrame.origin.x-=deltaX;
NSLog(@"The backgroundViewframe position, frameposition is %f and %f",fabsf(backFrame.origin.x),fabsf(myFrame.origin.x));
}
[tempView setFrame:backFrame];
[cell setFrame:myFrame];
}
else
{
NSLog(@"You have swiped right");
CGFloat movedDistance=currentPosition.x-gestureStartPoint.x;
NSLog(@"The movedDistance is %f",movedDistance);
if(movedDistance<=160)
{
myFrame.origin.x += deltaX;
backFrame.origin.x-=deltaX;
}
[cell setFrame:myFrame];
}
if ([swipeDelegate respondsToSelector:@selector(tableView:didSwipeCellAtIndexPath:)])
{
[swipeDelegate tableView:self didSwipeCellAtIndexPath:[self indexPathForRowAtPoint:gestureStartPoint]];
}
[self setIndexOfVisibleBackView:[self indexPathForCell:cell]];
}
[self setScrollEnabled:YES];
}
else
{
[super touchesMoved:touches withEvent:event];
}
}
-(UIView *) addnewViewatPoint:(NSSet *)touches withEvent:event
{
UIView *backgroundView=[[[UIView alloc] initWithFrame: CGRectMake(10.0, 30.0, 200.0, 25.0)] autorelease];
[backgroundView setBackgroundColor:[UIColor redColor]];
[backgroundView setUserInteractionEnabled:NO];
backgroundView.tag=101;
return backgroundView;
}
- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event
{
if ([self supportsSwipingForCellAtPoint:gestureStartPoint])
{
TISwipeableTableViewCell * cell = (TISwipeableTableViewCell *)[self cellForRowAtIndexPath:[self indexPathForRowAtPoint:gestureStartPoint]];
if(isTouchesMoved==NO)
{
[self.delegate tableView:self didSelectRowAtIndexPath:[self indexPathForCell:cell]];
[self touchesCancelled:touches withEvent:event];
}
[UIView beginAnimations:nil context:NULL];
if(isTouchesMoved==YES)
{
[cell setFrame:prevLoc];
[tempView setFrame:prevLocBack];
isTouchesMoved=NO;
}
moved=TRUE;
[UIView commitAnimations];
[self touchesCancelled:touches withEvent:event];
//[super touchesEnded:touches withEvent:event];
}
else
{
[super touchesEnded:touches withEvent:event];
}
}
您能否將需要固定的uiview添加到表下方的視圖中?
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.