This article introduces the new properties of objective-C language, where category allows you to add your own methods to existing classes, the Protocol is a special Inheritance Method in objective-C language, similar to interfaces in Java. Next, I will first introduce their functions and analyze them with examples in the project.
I. Category
Sometimes we need to add some methods to a defined class, instead of rewriting the class. For example, when the project is large, the Code volume is large, or the class contains many methods, other code has called this class to create an object and use this class method, you can use a category to expand the class.
Note: classes can only be extended, rather than member variables.
Instance analysis:
1. Purpose: In my project, I need to compress the image. In this case, I want to extend the uiimage class by category and increase the image compression method.
2. Category definition:
Category. h declaration File
# Import <Foundation/Foundation. h>
@ Interface uiimage (uiimageext)
// This method is the image compression method I added.
-(Uiimage *) imagebyscalingandcroppingforsize :( cgsize) targetsize;
@ End
Category. m implementation file
# Import "uiimageext. H"
@ Implementation uiimage (uiimageext)
-(Uiimage *) imagebyscalingandcroppingforsize :( cgsize) targetsize
{
Uiimage * sourceimage = self;
Uiimage * newimage = nil;
Cgsize imagesize = sourceimage. size;
Cgfloat width = imagesize. width;
Cgfloat Height = imagesize. height;
Cgfloat targetwidth = targetsize. width;
Cgfloat targetheight = targetsize. height;
Cgfloat scalefactor = 0.0;
Cgfloat scaledwidth = targetwidth;
Cgfloat scaledheight = targetheight;
Cgpoint thumbnailpoint = cgpointmake (0.0, 0.0 );
If (cgsizeequaltosize (imagesize, targetsize) = No)
{
Cgfloat widthfactor = targetwidth/width;
Cgfloat heightfactor = targetheight/height;
If (widthfactor> heightfactor)
Scalefactor = widthfactor; // scale to fit height
Else
Scalefactor = heightfactor; // scale to fit width
Scaledwidth = width * scalefactor;
Scaledheight = height * scalefactor;
// Center the image
If (widthfactor> heightfactor)
{
Thumbnailpoint. Y = (targetheight-scaledheight) * 0.5;
}
Else if (widthfactor
{
Thumbnailpoint. x = (targetwidth-scaledwidth) * 0.5;
}
}
Uigraphicsbeginimagecontext (targetsize); // This will crop
Cgrect thumbnailrect = cgrectzero;
Thumbnailrect. Origin = thumbnailpoint;
Thumbnailrect. Size. width = scaledwidth;
Thumbnailrect. Size. Height = scaledheight;
[Sourceimage drawinrect: thumbnailrect];
Newimage = uigraphicsgetimagefromcurrentimagecontext ();
If (newimage = nil)
Nslog (@ "cocould not scale image ");
// Pop the context to get back to the default
Uigraphicsendimagecontext ();
Return newimage;
}
@ End
3. How to Use the Category I have extended the uiimage above. The following shows how to call this method in my project:
// Display the Image Based on the Image Tag
-(Void) showphotobyserialnumber :( INT) imagetag;
{
// This largeimagearray is nsmutablearray type. It stores the image storage path and obtains the uiimage according to the path.
Uiimage * IMG = [uiimage imagewithcontentsoffile: [self. largeimagearray objectatindex: imagetag];
// Myscrollview is my custom scrollview, which aims to make the scrollview respond to click events. I will elaborate on how to customize the scrollview in a later blog.
Myscrollview * scrview = [[myscrollview alloc] initwithframe: cgrectmake (340 * imagetag, 0,320,480)];
Scrview. Host = self;
// This statement calls the category. You can call imagebyscalingandcroppingforsize through the uiimage Instance Object: Category
Scrview. Image = [img imagebyscalingandcroppingforsize: cgsizemake (320.0, 480.0)];
Scrview. Tag = imagetag + 100;
// Insert the above scrview to the imagescrollview. The imagescrollview is of the uiscrollview type.
[Self. imagescrollview addsubview: scrview];
[Scrview release];
}
Ii. Agreement
Similar to interfaces in Java, Protocol defines a set of methods without specific implementation,
Only the conform to or adopt classes that use these protocols provide their own implementations. The Protocol is not the class itself. They only define interfaces that other objects have the responsibility to implement. When you implement the Protocol method in your own class, your class complies with this Protocol, and the method declared in the Protocol can be implemented by any class.
1. The syntax structure of the protocol is as follows:
@ Protocol protocolname // protocol name
Methoddeclaration; // method name
@ End 2. When using the Protocol and declaring a class, the syntax is as follows:
@ Interface classname: parentclassname
<Protocolname>
Then, in the implementation file of this class, the method to implement this protocol is methoddeclaration.
3. instance analysis
The following describes how to use the Protocol in my project. Here we only need to talk about my own protocol.
1) Purpose of the instance: To enable the uiimageview on the scrollview to respond to the Click Event
2) Protocol definition:
Define the protocol in the. h declaration file of the class
# Import <Foundation/Foundation. h>
// The imagetouchdelegate Protocol
@ Protocol imagetouchdelegate
// Method declared in the Protocol
-(Void) imagetouch :( nsset *) touches withevent :( uievent *) event whichview :( ID) imageview;
@ End
@ Interface imagetouchview: uiimageview {
ID <imagetouchdelegate> delegate;
}
@ Property (nonatomic, assign) ID <imagetouchdelegate> delegate;
@ End
The. m implementation file for this class is as follows:
# Import "imagetouchview. H"
@ Implementation imagetouchview
@ Synthesize delegate;
-(ID) initwithframe :( cgrect) Frame
{
If (Self = [Super initwithframe: frame])
{
[Self setuserinteractionenabled: Yes];
}
Return self;
}
-(Bool) touchesshouldbegin :( nsset *) touches withevent :( uievent *) event incontentview :( uiview *) view
{
Return yes;
}
-(Void) touchesbegan :( nsset *) touches withevent :( uievent *) event
{
[Delegate imagetouch: touches withevent: Event whichview: Self];
}
@ End
3) how to use the Protocol?
I have defined imagetouchdelegate. How can I use this protocol? When a class uses this protocol, you can list the protocol name in the class declaration. If you want to use multiple protocols, separate them with commas (,) in the brackets. When the class adopts this protocol, it must be declared in addition to the. h file, and the methods in the declared protocol must be implemented in its. m implementation file. The preceding example demonstrates how to implement the Protocol in my project.
In my class, first use the imagetouchdelegate protocol in the. h declaration file.
# Import <uikit/uikit. h>
// Introduce the header file of the definition protocol imagetouchdelegate
# Import "imagetouchview. H"
// Place the protocol name in the angle brackets behind the parent header. If multiple protocols exist, separate them with commas (,).
@ Interface photoonshotviewcontroller: uiviewcontroller <imagetouchdelegate> {
}
@ End
The following describes how to implement the Protocol definition in the. m implementation file of the class.
# Import "photoonshotviewcontroller. H"
@ Implementation photoonshotviewcontroller
// Implement the methods defined in the Protocol,
-(Void) imagetouch :( nsset *) touches withevent :( uievent *) event whichview :( ID) imageview {
}
@ End
Categories and protocols are widely used and useful in iOS development. It is necessary to master them!
Now, the simple application of categories and Protocols has been described. If you have any questions, please feel free to contact us!