可在 App Store 风格中使用的嵌入购买按钮,具有适当的动画。在 Interface Builder 中可配置标题。
创建 AvePurchaseButton 的实例。设置 normalTitle、confirmationTitle 并处理 UIControlEventTouchUpInside 事件。要更改状态,使用 -[AvePurchaseButton setButtonState:animated:]。
- (void)addPurchaseButton {
AvePurchaseButton* button = [[AvePurchaseButton alloc] initWithFrame:CGRectZero];
[button addTarget:self action:@selector(purchaseButtonTapped:) forControlEvents:UIControlEventTouchUpInside];
button.buttonState = AvePurchaseButtonStateNormal;
button.normalTitle = @"$ 2.99";
button.confirmationTitle = @"BUY";
[button sizeToFit];
[self.view addSubview:button];
}
- (void)purchaseButtonTapped:(AvePurchaseButton*)button {
switch(button.buttonState) {
case AvePurchaseButtonStateNormal:
[button setButtonState:AvePurchaseButtonStateConfirmation animated:YES];
break;
case AvePurchaseButtonStateConfirmation:
// start the purchasing progress here, when done, go back to
// AvePurchaseButtonStateProgress
[button setButtonState:AvePurchaseButtonStateProgress animated:YES];
[self startPurchaseWithCompletionHandler:^{
[button setButtonState:AvePurchaseButtonStateNormal animated:YES];
}];
break;
case AvePurchaseButtonStateProgress:
break;
}
}