Fading Coder

One Final Commit for the Last Sprint

Home > Tech > Content

iOS UI Controls: Button Implementation, Basic Animations, Transform Properties, and Image Views

Tech 1

UIButton Fundamentals

UIButton positioning and sizing can be managed through several properties:

  • Position modification: Use .center property to access CGPoint values for x and y coordinate adjustments
  • Size modification: Access bounds property to rertieve CGRect, then modify size.height for height and size.width for width
  • Alternative approach: Utilize frame property for simultaneous position and dimension control

Creating UIButton Programmatically

// Initialize button instance
CustomButton *actionBtn = [[UIButton alloc] init];

// Add button to parent view
[self.view addSubview:actionBtn];

// Configure button dimensions and position
actionBtn.frame = CGRectMake(100, 100, 100, 100);

// Set background images for different states
[actionBtn setBackgroundImage:[UIImage imageNamed:@"button_normal"] forState:UIControlStateNormal];
[actionBtn setBackgroundImage:[UIImage imageNamed:@"button_pressed"] forState:UIControlStateHighlighted];

// Configure text and color for various states
[actionBtn setTitle:@"Tap Me" forState:UIControlStateNormal];
[actionBtn setTitleColor:[UIColor redColor] forState:UIControlStateNormal];

[actionBtn setTitle:@"Release" forState:UIControlStateHighlighted];
[actionBtn setTitleColor:[UIColor blueColor] forState:UIControlStateHighlighted];

// Register touchUpInside event handler
[actionBtn addTarget:self action:@selector(handleButtonClick:) forControlEvents:UIControlEventTouchUpInside];

// Event handler implementation
- (void)handleButtonClick:(UIButton *)sender {
    NSLog(@"Button was pressed");
}

Animation Techniques

Two primary approaches exist for creating simple animations:

Legacy Animation Syntax

[UIView beginAnimations:nil context:nil]; // Start animation block
[UIView setAnimationDuration:1]; // Define animation duration
// Animation code goes here
[UIView commitAnimations]; // Commit changes

Modern Block-Based Approach

[UIView animateWithDuration:0.5 animations:^{
    // Animation code implementation
}];

Transform Operations

The transform property enables modification of position, scaling, and rotation:

Transformation Methods

// Creation methods
CGAffineTransform CGAffineTransformMakeTranslation(CGFloat dx, CGFloat dy);
CGAffineTransform CGAffineTransformMakeScale(CGFloat scaleX, CGFloat scaleY);
CGAffineTransform CGAffineTransformMakeRotation(CGFloat radians); // Note: uses radians, not degrees

// Accumulation methods
CGAffineTransform CGAffineTransformTranslate(CGAffineTransform transform, CGFloat dx, CGFloat dy);
CGAffineTransform CGAffineTransformScale(CGAffineTransform transform, CGFloat scaleX, CGFloat scaleY);
CGAffineTransform CGAffineTransformRotate(CGAffineTransform transform, CGFloat radians);

// Reset transformation
view.transform = CGAffineTransformIdentity;

Practical Example Implementation

@interface ViewController ()
@property (weak, nonatomic) IBOutlet UIButton *interactiveButton;

// Action methods
- (IBAction)performTranslation;
- (IBAction)performRotation;
- (IBAction)performScaling;
- (IBAction)resetTransform:(id)sender;
@end

@implementation ViewController

- (IBAction)performTranslation {
    // Translate based on current position
    self.interactiveButton.transform = CGAffineTransformTranslate(self.interactiveButton.transform, 0, 50);
}

- (IBAction)performRotation {
    [UIView animateWithDuration:2.5 animations:^{
        self.interactiveButton.transform = CGAffineTransformRotate(self.interactiveButton.transform, -M_PI_4);
        self.interactiveButton.transform = CGAffineTransformTranslate(self.interactiveButton.transform, 0, 50);
        self.interactiveButton.transform = CGAffineTransformScale(self.interactiveButton.transform, 1.5, 1.5);
    }];
}

- (IBAction)performScaling {
    self.interactiveButton.transform = CGAffineTransformScale(self.interactiveButton.transform, 1.5, 1.5);
}

- (IBAction)resetTransform:(id)sender {
    self.interactiveButton.transform = CGAffineTransformIdentity;
}
@end

UIImageView Implementation

Animation Example with Sequential Images

@interface ViewController ()
@property (weak, nonatomic) IBOutlet UIImageView *animatedImageView;

- (IBAction)triggerDrinkAnimation;
- (IBAction)triggerFartAnimation;
- (IBAction)triggerKnockoutAnimation;
@end

@implementation ViewController

- (IBAction)triggerDrinkAnimation {
    [self executeAnimationWithFrameCount:81 imageNamePrefix:@"drink"];
}

- (IBAction)triggerFartAnimation {
    [self executeAnimationWithFrameCount:28 imageNamePrefix:@"fart"];
}

- (IBAction)triggerKnockoutAnimation {
    [self executeAnimationWithFrameCount:81 imageNamePrefix:@"knockout"];
}

- (void)executeAnimationWithFrameCount:(NSInteger)frameCount imageNamePrefix:(NSString *)prefix {
    // Prevent multiple simultaneous animations
    if (self.animatedImageView.isAnimating) {
        return;
    }
    
    // Build image sequence array
    NSMutableArray *imageSequence = [NSMutableArray array];
    
    for (NSInteger index = 0; index < frameCount; index++) {
        NSString *imageName = [NSString stringWithFormat:@"%@_%02ld.png", prefix, index];
        NSString *fullImagePath = [[NSBundle mainBundle] pathForResource:imageName ofType:nil];
        UIImage *currentImage = [UIImage imageWithContentsOfFile:fullImagePath];
        [imageSequence addObject:currentImage];
    }
    
    // Configure animation parameters
    self.animatedImageView.animationImages = imageSequence;
    self.animatedImageView.animationDuration = self.animatedImageView.animationImages.count * 0.1;
    self.animatedImageView.animationRepeatCount = 1;
    
    // Start animation
    [self.animatedImageView startAnimating];
    
    // Schedule cleanup after animation completes
    [self.animatedImageView performSelector:@selector(setAnimationImages:) 
                                  withObject:nil 
                                  afterDelay:self.animatedImageView.animationDuration];
}
@end

The animation system loads sequential images into an array, assigns them to the UIImageView's animationImages property, sets duration and repeat parameters, then executes the animation sequence.

Related Articles

Understanding Strong and Weak References in Java

Strong References Strong reference are the most prevalent type of object referencing in Java. When an object has a strong reference pointing to it, the garbage collector will not reclaim its memory. F...

Comprehensive Guide to SSTI Explained with Payload Bypass Techniques

Introduction Server-Side Template Injection (SSTI) is a vulnerability in web applications where user input is improper handled within the template engine and executed on the server. This exploit can r...

Implement Image Upload Functionality for Django Integrated TinyMCE Editor

Django’s Admin panel is highly user-friendly, and pairing it with TinyMCE, an effective rich text editor, simplifies content management significantly. Combining the two is particular useful for bloggi...

Leave a Comment

Anonymous

◎Feel free to join the discussion and share your thoughts.