Jak mogę utworzyć niestandardowy interfejs użytkownika w systemie iOS?
Jak mogę utworzyć własny UIActivity
w iOS?
Powodem, dla którego chcę to jest dodanie przycisku recenzji aplikacji w jednej z moich aplikacji, który przenosi użytkownika do sekcji recenzji w App Store. Jak mogę stworzyć taki custom UIActivity
?
4 answers
Najpierw Utwórz pliki. Wybrałem nazwę mojego ActivityViewCustomActivity
Make ActivityViewCustomActivity.h wygląda tak:
#import <UIKit/UIKit.h>
@interface ActivityViewCustomActivity : UIActivity
@end
Make ActivityViewCustomActivity.m wygląda tak:
#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
Tak wyglądał mój wizerunek: Tutaj jest .PSD zrobiłem: -- złośliwy link USUNIĘTY -- A oto oryginalny 250 px .png http://i.imgur.com/pGWVj.png
Teraz w kontrolerze widoku zrób to:
#import "ActivityViewCustomActivity.h"
And now wherever you chcesz wyświetlić swój 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];
}
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2015-09-26 22:02:57
Oto moja wersja Swift - potrzebowałem wielu niestandardowych akcji, więc stworzyłem tę klasę. Nie potrzeba delegatów ani protokołów.
1. Dodaj klasę niestandardową
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 Użyj kontrolera widoku
Dołączyłem go do UIBarButtonItem, który wywołuje następujące
@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)
}
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2015-05-27 02:33:19
My Swift 3 implementacja oparta na DogCoffee ' S:
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()
}
}
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2016-12-16 16:02:01
Oto przykład tworzenia interfejsu kompozytora wiadomości e-mail przy użyciu metody-activityViewController interfejsu UIActivity. To pokazuje, jak umieścić UIKit viewController lub własny własny viewController w dowolnym celu. Wypiera metodę-performActivity.
#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 = @"[email protected]";
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
Zauważ, że-activityDidFinish jest wywoływany z delegata mail composer po tym, jak użytkownik odrzucił interfejs e-mail. Jest to wymagane, aby uzyskać interfejs UIActivityViewController do zniknij. Jeśli napiszesz swój własny viewController, będzie on potrzebował metody delegata, którą wywoła po zakończeniu i będziesz musiał uczynić swoją podklasę UIActivity delegatem.
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2015-12-29 19:24:16