滑动视图中的按钮未正确居中

时间:2013-05-13 14:30:16

标签: ios objective-c cocoa-touch

我有一个名为SideSwipeView的视图,当它向左滑动时会替换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

UISwipeGestureRecognizerDirectionLeft方法几乎与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];
}
}

0 个答案:

没有答案