Custom Instrument Control Based on iPhone SDK Development

Source: Internet
Author: User
Tags uikit

IPhone SDK development Basics
Custom instrument controls
In iOS development, because of the needs of the program, sometimes you need to draw the interface control that the iPhone SDK does not provide by yourself, usually using quartzcore. framework to draw the various graphics you need. Here we implement a circular "dashboard" control, as shown in Figure 3-48, you can set various system parameters required by the program by rotating the pointer of the meter control.
 

The control uses two uiviews to implement the instrument control and the cgaffinetransform class to rotate the instrument pointer. The control is implemented in the uidialview class. The definition of the uidialview class is as follows.
// Uidialview. h
# Import <uikit/uikit. h>

@ Protocol uidialviewdelegate
@ Optional
-(Void) dialvalue :( INT) Tag value :( float) value;
@ End

@ Interface uidialview: uiview {
ID <uidialviewdelegate> delegate;
Nstmer * timer;
Uiimageview * IV;
Float maxvalue, minvalue;
Cgaffinetransform initialtransform;
Float currentvalue;
}
@ Property (nonatomic, assign) ID <uidialviewdelegate> delegate;
@ Property cgaffinetransform initialtransform;
@ Property float currentvalue;

@ End
In the implementation file of the uidialview class, use the init () method to read the background and pointer of the image file initialization control. The Code is as follows.
// Uidialview. m
# Import "uidialview. H"

@ Interface uidialview ()
-(Void) spin :( nstimer *) timer;
-(Float) gooddegrees :( float) degrees;
@ End

# Define degreestoradians (degrees) (m_pi * degrees/180.0)
# Define radianstodegrees (radians) (radians * 180/m_pi)

Static cgpoint delta;
Static float deltaangle;
Static float currentangle;

@ Implementation uidialview
@ Synthesize initialtransform, currentvalue;

-(Void) dealloc {
[IV Release];
[Super dealloc];
}

@ Synthesize delegate;

-(ID) Init {
If (Self = [Super init]) {

Self. userinteractionenabled = yes;
IV = [[uiimageview alloc] initwithimage: [uiimage imagenamed: @ "knob. PNG"];

Uiimage * backgroundtile = [uiimage imagenamed: @ "clock.png"];
Uicolor * backgroundpattern = [[uicolor alloc] initwithpatternimage: backgroundtile];
Self. contentmode = uiviewcontentmodecenter;
[Self setbackgroundcolor: backgroundpattern];
[Backgroundpattern release];

Iv. backgroundcolor = [uicolor clearcolor];
Iv. autoresizessubviews = yes;
Self. Frame = cgrectmake (0, 0, IV. Frame. Size. Width, IV. Frame. Size. Height );

[Self addsubview: IV];
[Self bringsubviewtofront: IV];
[IV Release];

Currentvalue = 0;
Currentangle = 0;
Deltaangle = 0.0;
}
Return self;
}
Capture the user's touch point location in the touchesbegan () method of uiview, and calculate the control initialization angle using the atan2 () function based on this location. The Code is as follows.
-(Void) touchesbegan :( nsset *) touches withevent :( uievent *) event {
Uitouch * thistouch = [touches anyobject];
Delta = [thistouch locationinview: Self];
 
Float dx = delta. X-IV. Center. X;
Float DY = delta. Y-IV. Center. Y;
Deltaangle = atan2 (dy, dx );
Initialtransform = IV. Transform;
}
In the user's rotation process, you can set the transform attribute of the uiview object to rotate the Instrument Control pointer along with the rotation of the user's finger. The Code is as follows.
-(Void) touchesmoved :( nsset *) touches withevent :( uievent *) event {
Uitouch * Touch = [touches anyobject];
Cgpoint Pt = [Touch locationinview: Self];
 
Float dx = pt. X-IV. Center. X;
Float DY = pt. Y-IV. Center. Y;
Float ang = atan2 (dy, dx );

If (deltaangle = 0.0 ){
Deltaangle = Ang;
Initialtransform = IV. Transform;
} Else {
Float angledif = deltaangle-Ang;
Cgaffinetransform newtrans = cgaffinetransformrotate (initialtransform,-angledif );
Iv. Transform = newtrans;

Float diffvalue = [self gooddegrees: radianstodegrees (angledif)];
Currentvalue = maxvalue-(maxvalue-minvalue)/300) * diffvalue;
If (currentvalue> 100) currentvalue = 100;
}
If (delegate! = Nil ){
[Delegate dialvalue: Self. Tag value: currentvalue];
}
}
The customer obtains the control notification message by implementing the dialvalue () method of the uidialviewdelegate interface protocol. The Code is as follows.
// Dialviewcontroller. h
# Import <uikit/uikit. h>
# Import "uidialview. H"

@ Interface dialviewcontroller: uiviewcontroller <uidialviewdelegate> {
Uidialview * dialview;
Uilabel * mylabel;
}

-(Void) dialvalue :( INT) Tag value :( float) value {
Nsstring * STR = [nsstring stringwithformat: @ "%. 1f", V * 100];
[Mylabel into mselector: @ selector (settext :) withobject: Str];
}
For complete xcode project source code files related to this section, see the dialcontrol project on the CD attached to this book.

 

This article is excerpted from the book IOS software development secrets: iPhone & iPad enterprise applications and game development.
The book IOS software development secrets: iPhone & iPad enterprise application and game development has been officially published by the Electronics Industry Publishing House, and this book is published by Yu Bin.

Purchase address:

Interactive publishing network: http://product.china-pub.com/198191

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.