How to get Parallax Effect on UIButton in tvOS?
Solved my problem -
Set your LSR or Apple TV image stack in your UIButton
's setImage
property for the UIControlState.Focused
state.
Set the imageView?.clipsToBounds = false
on the UIButton
.
Set newButton.imageView?.adjustsImageWhenAncestorFocused = true
on the UIButton
.
I have got the following code to work as expected:
for button in 1...3 {
let buttonUnpressedTexture = UIImage(contentsOfFile:NSBundle.mainBundle().resourcePath!.stringByAppendingPathComponent("startReadingUnpressed.png"))!
let buttonPressedTexture = UIImage(contentsOfFile:NSBundle.mainBundle().resourcePath!.stringByAppendingPathComponent("startReadingPressed.png"))!
let newButton = UIButton(type: .Custom)
newButton.frame = buttonRects[button - 1]
newButton.setImage(buttonUnpressedTexture, forState: UIControlState.Normal)
newButton.setImage(buttonPressedTexture, forState: UIControlState.Highlighted)
newButton.setImage(UIImage(named: "StartReadingButton"), forState: UIControlState.Focused)
newButton.imageView?.clipsToBounds = false
newButton.imageView?.adjustsImageWhenAncestorFocused = true
newButton.addTarget(self, action: "buttonPressed:", forControlEvents: UIControlEvents.PrimaryActionTriggered)
newButton.tag = button
newButton.canBecomeFocused()
self.addSubview(newButton)
}
"StartReadingButton" is an Apple TV image stack in my images.xcassets catalog.
tvOS: Creating parallax effect on UICollectionViewCell
Just found an answer. Hope this helps someone else:
- (UICollectionViewCell *)collectionView:(UICollectionView *)cv cellForItemAtIndexPath:(NSIndexPath *)indexPath {
UICollectionViewCell *cell = [cv dequeueReusableCellWithReuseIdentifier:@"CameraCell" forIndexPath:indexPath];
UIImage *image = [_cameras objectAtIndex:indexPath.row];
UIImageView *imageView = [[UIImageView alloc] initWithImage:image];
imageView.userInteractionEnabled = YES;
imageView.adjustsImageWhenAncestorFocused = YES;
imageView.frame = CGRectMake(0, 0, 853, 560);
[cell addSubview:imageView];
return cell;
}
tvOS - UIButton doesn't animate when focused
Calling that UIButton()
initializer will get you a 'custom' button, when what you really want is a 'system' button, like so:
let button = UIButton(type: .System)
Custom buttons don't get any built-in appearance at all, while system buttons do.
tvOS: Creating UICollectionViewCell effect like main screen
Found the problem. Clip Sub Views
should be disabled in UIImageView
otherwise parallax effect will not work properly. However collection view cell's clip Sub Views can be (must be, in my case) enabled.
Reference: https://developer.apple.com/tvos/human-interface-guidelines/
in code:
[cell.imageView setClipsToBounds:NO];
in interface builder:
How to set color of UIButton type: .system for tvOS in Swift
buttonA.layer.cornerRadius = 10
Can we stop or remove animation effect on focus with UIButton and give other border effect tvOS
First of all you have to set your button type to Custom type. By custom type you will get no more system animations, so you have to do all animations by yourself.
Then you can implement didUpdateFocusInContext method either in UIViewController or you can make your own UIButton subclass if there are more button types on a single screen.
Here is a code that I use in my UIButton subclass. This will provide the button enlargement along with red border on focus and will get to normal state on focus loss.
let scale = 1.1
layer.borderColor = UIColor.redColor().CGColor
override func didUpdateFocusInContext(context: UIFocusUpdateContext, withAnimationCoordinator coordinator: UIFocusAnimationCoordinator) {
if context.nextFocusedView == self {
coordinator.addCoordinatedAnimations({ () -> Void in
self.transform = CGAffineTransformMakeScale(scale, scale)
self.layer.borderWidth = 2
}, completion: nil)
}
else {
coordinator.addCoordinatedAnimations({ () -> Void in
self.transform = CGAffineTransformIdentity
self.layer.borderWidth = 0
}, completion: nil)
}
}
Related Topics
Combining Scenekit and Spritekit in a Single Screen
How to Properly Map JSON Properties to Model Properties in Realm.Create
Restrictions Around Protocols and Generics in Swift
Swift-Animate Cashapelayer Stroke Color
How to Draw Something on a PDF in Swift
Calling Asynchronous Method Inside For-Loop
Thread Safety of Method Calls on "Shared" Static Constant Property
Qlpreviewcontroller Showing File Then Going Blank in Swiftui
Alamofire Nonblocking Connection
Loading Many UIimages from Disk Blocks Main Thread
Prepareforsegue from a UIbutton in a Custom Prototype Cell
Nsattributedstring Boundingrect Returns Wrong Height
Wkwebview - Update HTML Tags from Swiftui Textfields
Nsdocumentcontroller.Opendocument Not Allowing Selection of Custom File Type
How to Find The Top 3 Maximum Values in a Swift Dictionary
Will a a Weak Wrapper Class Throw Off a Hashable Based Collection When The Wrapped Value Is Set Nil
Set Maximum Characters (To One) in a Nstextfield in Swift
Why Is a Firestore Listener Returning .Added Twice When a Single Document Is Added