我有一个名为的视图,它在向左滑动时SideSwipeView
替换 a 。UITableViewCell
当SideSwipeView
出现六个按钮时应该出现在SideSwipeView
. 出现了 6 个按钮,但它们从未位于正确的位置,我如何才能将它们完美地置于视图中?
- (void) setupSideSwipeView
{
// Iterate through the button data and create a button for each entry
CGFloat leftEdge = BUTTON_LEFT_MARGIN;
for (NSDictionary* buttonInfo in buttonData)
{
// Create the button
UIButton* button = [UIButton buttonWithType:UIButtonTypeCustom];
// Make sure the button ends up in the right place when the cell is resized
button.autoresizingMask = UIViewAutoresizingFlexibleRightMargin | UIViewAutoresizingFlexibleLeftMargin | UIViewAutoresizingFlexibleBottomMargin | UIViewAutoresizingFlexibleTopMargin;
// Get the button image
UIImage* buttonImage = [UIImage imageNamed:[buttonInfo objectForKey:@"image"]];
// Set the button's frame
button.frame = CGRectMake(leftEdge, sideSwipeView.center.y - buttonImage.size.height/2.0, buttonImage.size.width, buttonImage.size.height);
// Add the image as the button's background image
// [button setBackgroundImage:buttonImage forState:UIControlStateNormal];
UIImage* grayImage = [self imageFilledWith:[UIColor colorWithWhite:0.9 alpha:1.0] using:buttonImage];
[button setImage:grayImage forState:UIControlStateNormal];
// Add a touch up inside action
[button addTarget:self action:@selector(touchUpInsideAction:) forControlEvents:UIControlEventTouchUpInside];
// Keep track of the buttons so we know the proper text to display in the touch up inside action
[buttons addObject:button];
// Add the button to the side swipe view
[self.sideSwipeView addSubview:button];
// Move the left edge in prepartion for the next button
leftEdge = leftEdge + buttonImage.size.width + BUTTON_SPACING;
}
}
向右或向左滑动的方法。该swipeLeft
方法几乎与swipeRight
仅使用 withUISwipeGestureRecognizerDirectionLeft
而不是相同UISwipeGestureRecognizerDirectionRight
// Called when a right swipe occurred
- (void)swipeRight:(UISwipeGestureRecognizer *)recognizer
{
[self swipe:recognizer direction:UISwipeGestureRecognizerDirectionRight];
}
// Handle a left or right swipe
- (void)swipe:(UISwipeGestureRecognizer *)recognizer direction:(UISwipeGestureRecognizerDirection)direction
{
if (recognizer && recognizer.state == UIGestureRecognizerStateEnded)
{
// Get the table view cell where the swipe occured
CGPoint location = [recognizer locationInView:self.tableView];
NSIndexPath* indexPath = [self.tableView indexPathForRowAtPoint:location];
UITableViewCell* cell = [self.tableView cellForRowAtIndexPath:indexPath];
// If we are already showing the swipe view, remove it
if (cell.frame.origin.x != 0)
{
[self removeSideSwipeView:YES];
return;
}
// Make sure we are starting out with the side swipe view and cell in the proper location
[self removeSideSwipeView:NO];
// If this isn't the cell that already has thew side swipe view and we aren't in the middle of animating
// then start animating in the the side swipe view
if (cell!= sideSwipeCell && !animatingSideSwipe)
[self addSwipeViewTo:cell direction:direction];
}
}