如何在iOS中创建自定义UIActivity?
如何在iOS中创建自定义如何在iOS中创建自定义UIActivity?,ios,objective-c,uiactivityviewcontroller,uiactivity,Ios,Objective C,Uiactivityviewcontroller,Uiactivity,如何在iOS中创建自定义UIActivity 我希望这样做的原因是在我的一个应用程序中添加一个Review应用程序按钮,将用户带到应用程序商店中的Review部分。如何创建这样一个自定义的UIActivity?首先,创建文件。我选择将我的活动命名为ViewCustomActivity 使ActivityViewCustomActivity.h如下所示: #import <UIKit/UIKit.h> @interface ActivityViewCustomActivity : U
UIActivity
我希望这样做的原因是在我的一个应用程序中添加一个Review应用程序按钮,将用户带到应用程序商店中的Review部分。如何创建这样一个自定义的
UIActivity
?首先,创建文件。我选择将我的活动命名为ViewCustomActivity
使ActivityViewCustomActivity.h如下所示:
#import <UIKit/UIKit.h>
@interface ActivityViewCustomActivity : UIActivity
@end
#import "ActivityViewCustomActivity.h"
@implementation ActivityViewCustomActivity
- (NSString *)activityType
{
return @"yourappname.Review.App";
}
- (NSString *)activityTitle
{
return @"Review App";
}
- (UIImage *)activityImage
{
// Note: These images need to have a transparent background and I recommend these sizes:
// iPadShare@2x should be 126 px, iPadShare should be 53 px, iPhoneShare@2x should be 100
// px, and iPhoneShare should be 50 px. I found these sizes to work for what I was making.
if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad)
{
return [UIImage imageNamed:@"iPadShare.png"];
}
else
{
return [UIImage imageNamed:@"iPhoneShare.png"];
}
}
- (BOOL)canPerformWithActivityItems:(NSArray *)activityItems
{
NSLog(@"%s", __FUNCTION__);
return YES;
}
- (void)prepareWithActivityItems:(NSArray *)activityItems
{
NSLog(@"%s",__FUNCTION__);
}
- (UIViewController *)activityViewController
{
NSLog(@"%s",__FUNCTION__);
return nil;
}
- (void)performActivity
{
// This is where you can do anything you want, and is the whole reason for creating a custom
// UIActivity
[[UIApplication sharedApplication] openURL:[NSURL URLWithString:@"itms-apps://ax.itunes.apple.com/WebObjects/MZStore.woa/wa/viewContentsUserReviews?type=Purple+Software&id=yourappid"]];
[self activityDidFinish:YES];
}
@end
这就是我的形象:
这是我制作的.PSD:--恶意链接已删除--
这是原始的250 px.png
现在,在视图控制器中执行以下操作:
#import "ActivityViewCustomActivity.h"
现在,无论您想在何处显示您的UIActivityViewController
:
NSString *textItem = @"Check out the yourAppNameHere app: itunes http link to your app here";
UIImage *imageToShare = [UIImage imageNamed:@"anyImage.png"];
NSArray *items = [NSArray arrayWithObjects:textItem,imageToShare,nil];
ActivityViewCustomActivity *aVCA = [[ActivityViewCustomActivity alloc]init];
UIActivityViewController *activityVC =
[[UIActivityViewController alloc] initWithActivityItems:items
applicationActivities:[NSArray arrayWithObject:aVCA]];
activityVC.excludedActivityTypes = @[UIActivityTypePostToWeibo, UIActivityTypeAssignToContact, UIActivityTypePrint, UIActivityTypeCopyToPasteboard, UIActivityTypeSaveToCameraRoll];
activityVC.completionHandler = ^(NSString *activityType, BOOL completed)
{
NSLog(@"ActivityType: %@", activityType);
NSLog(@"Completed: %i", completed);
};
if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad)
{
self.popoverController = [[UIPopoverController alloc] initWithContentViewController:activityVC];
CGRect rect = [[UIScreen mainScreen] bounds];
[self.popoverController
presentPopoverFromRect:rect inView:self.view
permittedArrowDirections:0
animated:YES];
}
else
{
[self presentViewController:activityVC animated:YES completion:nil];
}
这是我的Swift版本-我需要多个自定义操作,所以我创建了这个类。不需要代理或协议 1。添加自定义类
class ActivityViewCustomActivity: UIActivity {
var customActivityType = ""
var activityName = ""
var activityImageName = ""
var customActionWhenTapped:( (Void)-> Void)!
init(title: String, imageName:String, performAction: (() -> ()) ) {
self.activityName = title
self.activityImageName = imageName
self.customActivityType = "Action \(title)"
self.customActionWhenTapped = performAction
super.init()
}
override func activityType() -> String? {
return customActivityType
}
override func activityTitle() -> String? {
return activityName
}
override func activityImage() -> UIImage? {
return UIImage(named: activityImageName)
}
override func canPerformWithActivityItems(activityItems: [AnyObject]) -> Bool {
return true
}
override func prepareWithActivityItems(activityItems: [AnyObject]) {
// nothing to prepare
}
override func activityViewController() -> UIViewController? {
return nil
}
override func performActivity() {
customActionWhenTapped()
}
}
2在视图控制器中使用
我把它附在一个维吾尔布托教派上,它称之为
@IBAction func actionButtonPressed(sender: UIBarButtonItem) {
var sharingItems = [AnyObject]() // nothing to share...
let myCustomActivity = ActivityViewCustomActivity(title: "Mark Selected", imageName: "removePin") {
println("Do something")
}
let anotherCustomActivity = ActivityViewCustomActivity(title: "Reset All", imageName: "reload") {
println("Do something else")
}
let activityViewController = UIActivityViewController(activityItems:sharingItems, applicationActivities:[myCustomActivity, anotherCustomActivity])
activityViewController.excludedActivityTypes = [UIActivityTypeMail, UIActivityTypeAirDrop, UIActivityTypeMessage, UIActivityTypeAssignToContact, UIActivityTypePostToFacebook, UIActivityTypePrint, UIActivityTypeCopyToPasteboard, UIActivityTypeSaveToCameraRoll]
activityViewController.popoverPresentationController?.barButtonItem = sender
self.presentViewController(activityViewController, animated: true, completion: nil)
}
下面是一个使用UIActivity的-activityViewController方法设置电子邮件编写器界面的示例。这显示了如何为您选择的任何用途安装UIKit viewController或您自己的自定义viewController。它取代了性能法
#import <MessageUI/MessageUI.h>
#import <UIKit/UIKit.h>
@interface EPSuggestionsActivity : UIActivity <MFMailComposeViewControllerDelegate>
@end
@implementation EPSuggestionsActivity
....
- (UIViewController *)activityViewController{
MFMailComposeViewController *picker = [[MFMailComposeViewController alloc] init];
picker.mailComposeDelegate = self;
NSString *emailAddress = @"developer@apple.com";
NSArray *toRecipients = @[emailAddress];
[picker setToRecipients:toRecipients];
[picker setSubject:@"Suggestions"];
return picker;
}
#pragma mark - MFMailComposeViewControllerDelegate Method
- (void)mailComposeController:(MFMailComposeViewController*)controller didFinishWithResult:(MFMailComposeResult)result error:(NSError*)error {
[self activityDidFinish:YES]; // Forces the activityViewController to be dismissed
}
@end
#导入
#进口
@接口建议活动:UIActivity
@结束
@实施建议活动
....
-(UIViewController*)活动视图控制器{
MFMailComposeViewController*选择器=[[MFMailComposeViewController alloc]init];
picker.mailComposeDelegate=self;
NSString*电子邮件地址=@“developer@apple.com";
NSArray*toRecipients=@[emailAddress];
[picker setToRecipients:toRecipients];
[picker setSubject:@“建议”];
回程选择器;
}
#pragma-mark-MFMailComposeViewControllerDelegate方法
-(void)mailComposeController:(MFMailComposeViewController*)控制器未完成结果:(MFMailComposeResult)结果错误:(NSError*)错误{
[self-activityDidFinish:YES];//强制解除activityViewController
}
@结束
请注意,-activityDidFinish是在用户解除电子邮件界面后从mail composer委托调用的。这是使UIActivityViewController界面消失所必需的。如果您编写自己的viewController,它将需要一个完成时调用的委托方法,并且您必须使UIActivity的子类成为委托。MySwift 3基于DogCoffee的实现:
class ActivityViewCustomActivity: UIActivity {
// MARK: Properties
var customActivityType: UIActivityType
var activityName: String
var activityImageName: String
var customActionWhenTapped: () -> Void
// MARK: Initializer
init(title: String, imageName: String, performAction: @escaping () -> Void) {
self.activityName = title
self.activityImageName = imageName
self.customActivityType = UIActivityType(rawValue: "Action \(title)")
self.customActionWhenTapped = performAction
super.init()
}
// MARK: Overrides
override var activityType: UIActivityType? {
return customActivityType
}
override var activityTitle: String? {
return activityName
}
override class var activityCategory: UIActivityCategory {
return .share
}
override var activityImage: UIImage? {
return UIImage(named: activityImageName)
}
override func canPerform(withActivityItems activityItems: [Any]) -> Bool {
return true
}
override func prepare(withActivityItems activityItems: [Any]) {
// Nothing to prepare
}
override func perform() {
customActionWhenTapped()
}
}
根据苹果iPhone和iPodtouch手册,图像不应大于43×43点(对于带有视网膜显示器的设备,这相当于86×86像素)。对于iPad,图像不应大于55×55点(对于带有视网膜显示器的iPad,这相当于110×110像素)。苹果手册并非适用于所有情况。我选择的图像大小是经过反复试验确定的,完全符合我的需要。你能再次上传你的文件吗?看起来它们不再可用了。@DrMickeyLauer我在这里重新加载了.PSD文件:建议的图像大小在iOS 7中已更改-有关UIActivity,请参阅文档。对于iPhone和iPod touch,iOS 7上的图像应该是60乘60分。对于早期版本:不大于43乘以43点。对于iPad,图像应该是76乘76分;在早期版本上:不大于60乘60点。在带有视网膜显示器的设备上,每个方向的像素数都是原来的两倍。真糟糕,苹果默认没有这个功能。